【发布时间】:2014-01-29 03:49:27
【问题描述】:
我必须通过 python 脚本运行“提交”命令并根据其退出或返回状态打印一条消息。
代码如下:
import subprocess
msg = 'summary about commit'
commitCommand = 'hg commit -m "{}"'.format(msg)
p = subprocess.Popen(commitCommand, stdout=subprocess.PIPE)
output = p.communicate()[0]
if p.returncode:
print 'commit failed'
sys.exit()
else:
print 'Commit done'
这给了我以下错误:
Traceback (most recent call last):
File "script.py", line 66, in <module>
p = subprocess.Popen(commitCommand, stdout=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
如何纠正这个错误?
【问题讨论】:
-
import shlex; ...;subprocess.Popen(shlex.split(commitCommand), ...)。 Related question,Popen的文档(您应该阅读)。 -
@Bakuriu:当 OP 正在 构建字符串 时,为什么要使用
shlex.split()?
标签: python python-2.7