为什么我在运行 Python 程序时收到“sh: 1: Syntax error: Unterminatedquoted string”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18544112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why I got "sh: 1: Syntax error: Unterminated quoted string" when I run my Python program?
提问by maemual
I want to count how many lines of code I have written.
我想数一数我写了多少行代码。
Here is the Python code:
这是Python代码:
import os
import sys
EXT = ['.c','.cpp','.java','.py']
def main():
l = []
if os.path.isdir(sys.argv[1]):
for root, dirs, files in os.walk(sys.argv[1]):
l.extend([os.path.join(root, name) for name in files])
else:
l.append(sys.argv[1])
params = ["'"+p+"'" for p in l if os.path.splitext(p)[1] in EXT]
result = os.popen("wc -l %s "%" ".join(params)).read()
print result
if __name__ == '__main__':
main()
Before this, it was running as expected. But today, it give me this error:
在此之前,它按预期运行。但是今天,它给了我这个错误:
sh: 1: Syntax error: Unterminated quoted string
I don't know what happened.
我不知道发生了什么。
采纳答案by Johnsyweb
Your Python script is missing a shebangline. Add the following to the top of your file:
您的 Python 脚本缺少shebang行。将以下内容添加到文件顶部:
#!/usr/bin/env python
Then you should be able to run the following, assuming your script is at /path/to/your_script.py
and it has the executable bit set:
然后您应该能够运行以下命令,假设您的脚本位于/path/to/your_script.py
并且它设置了可执行位:
/path/to/your_script.py arg1 arg2 [...]
Alternatively:
或者:
python /path/to/your_script.py arg1 arg2 [...]
Update following comments
更新以下评论
I suspect what has changed is that a source file containing a '
in its name has been added to the directory you are checking and the shell is choking on this.
我怀疑发生了变化的是,'
名称中包含 a 的源文件已添加到您正在检查的目录中,并且 shell 对此感到窒息。
You could add the following function to your program:
您可以将以下功能添加到您的程序中:
def shellquote(s):
return "'" + s.replace("'", "'\''") + "'"
[Lifted from Greg Hewgill's answerto How to escape os.system() calls in Python?.]
[摘自Greg Hewgill对如何在 Python 中转义 os.system() 调用的回答?.]
And call it like this:
并这样称呼它:
params = [shellquote(p) for p in l if os.path.splitext(p)[1] in EXT]
回答by user650749
Looks like your Python program was parsed like a shell script. Add something like this at the header to indicate where your Python is:
看起来您的 Python 程序被解析为一个 shell 脚本。在标题中添加类似的内容以指示您的 Python 所在的位置:
#!/usr/bin/python
or you just run python a.py
.
或者你就跑python a.py
。
回答by tripleee
@Johnsyweb's updated answer seems to have the correct diagnostic, but the correct fix is to not use a shell to invoke wc
. Try something like this instead:
@Johnsyweb 的更新答案似乎有正确的诊断,但正确的解决方法是不使用 shell 调用wc
. 试试这样的:
cmd = ['/bin/wc', '-l'] # Need full path!
[cmd.extend(p) for p in l if os.path.splitext(p)[1] in EXT]
result = os.popen2(cmd).read()
Note that the subprocess
module is the recommended solution now. Switching to that requires a less intrusive change to your current code, though; see http://docs.python.org/2/library/subprocess.html#replacing-os-popen-os-popen2-os-popen3
请注意,该subprocess
模块现在是推荐的解决方案。但是,切换到它需要对当前代码进行较少侵入性的更改;见http://docs.python.org/2/library/subprocess.html#replacing-os-popen-os-popen2-os-popen3