尝试使用python脚本中的选项调用shell脚本。 ./ dropbox_uploader.sh -s download / test / pictures pictures / 这一行通过SSH运行正常但从python脚本调用时出错:
import subprocess
subprocess.call(['./dropbox_uploader.sh -s download /test/pictures pictures/'])
以下是错误消息:
Traceback (most recent call last):
File "sync.py", line 2, in <module>
subprocess.call(['./dropbox_uploader.sh -s download /test/pictures pictures/'])
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
答案 0 :(得分:2)
如果subprocess.call
的第一个参数是一个列表,它必须包含可执行文件和参数作为单独的项:
subprocess.call(['./dropbox_uploader.sh', '-s',
'download', '/test/pictures', 'pictures/'])
或者,也许更方便:
import shlex
cmd = './dropbox_uploader.sh -s download /test/pictures pictures/'
subprocess.call(shlex.split(cmd))
还有一个选项可以将解析和执行委托给shell:
cmd = './dropbox_uploader.sh -s download /test/pictures pictures/'
subprocess.call(cmd, shell=True)
(但请注意security warning)