的工作方式不同要按下面的代码运行在python脚本二进制:subprocess.call()的脚本和解释
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'前尝试'打印args' – thefourtheye
@thefourtheye'['dot','out.dot','-Tpng','-o','out.png']' – Alex