运行python脚本时,如何读取带空格的参数?

UPDATE :

看来我的问题是我正在通过 shell 程序脚本调用python脚本:

这有效:

> python script.py firstParam file\ with\ spaces.txt
# or
> python script.py firstParam "file with spaces.txt"

# script.py
import sys
print sys.argv

但是,不是通过脚本运行它时:

myscript.sh:
#!/bin/sh
python $@

打印: ['firstParam','file','with','spaces.txt']

但是我想要的是:
['firstParam','带空格的文件.txt']

最佳答案

使用"$@"代替:

#!/bin/sh
python "$@"

输出:
$ /tmp/test.sh /tmp/test.py firstParam "file with spaces.txt"
['/tmp/test.py', 'firstParam', 'file with spaces.txt']
/tmp/test.py定义为:

import sys
print sys.argv

10-04 09:58