【发布时间】:2014-02-21 06:16:04
【问题描述】:
希望通过以下代码从 python 脚本运行二进制文件:
def runner(output_file):
result = 1
try:
image_name = re.sub(r'\..*', '.png', output_file)
args = ['dot', output_file, '-Tpng', '-o', image_name]
result = subprocess.call(args)
if(result == 0):
print('Graph is rendered to {0}'.format(image_name))
except:
print('ERROR: Cannot run DOT. Please check your PATH')
return result
当我调用这个函数时,它返回0,一切似乎都正常,但是没有生成文件。
当我从 Python 解释器做同样的事情时:
Python 2.7.5+ (default, Sep 19 2013, 13:48:49)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> output_file = 'out.dot'
>>> image_name = 'out.png'
>>> args = ['dot', output_file, '-Tpng', '-o', image_name]
>>> subprocess.call(args)
文件生成成功。
我尝试设置错误的名称,但在脚本中出现错误(如预期的那样)。从脚本调用pwd 给了我一个正确的目录。使用硬编码值调用与使用变量调用相同。
我做错了什么?
【问题讨论】:
-
也许你应该使用 shell=True 运行?
-
在调用
subprocess.call之前尝试print args -
您是如何(以及在哪个目录下)运行脚本的?
-
此外,虽然这里似乎没有出现问题,但没有充分的理由使用裸
except:子句。我建议不要使用它,除非你有充分的理由这样做,因为它可以隐藏你不期望的异常。 -
@JayanthKoushik 不。不要不要使用
shell=True,尤其是在参数列表中。当shell=True列表的第一个元素作为整个命令行时,所有其他元素作为选项传递给shell。这显然 不是 OP 想要的。
标签: python python-2.7 subprocess