的工作方式不同要按下面的代碼運行在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