2014-12-02 163 views
-3

我使用Python生成了一个点文件。从Python代码执行脚本

每次生成文件时,我需要在终端中运行一个脚本,将其转换为pdf文件或图像。

这是脚本:

dot -Tpdf somefile.dot -o somefile.pdf 

我想知道是否有可能在当前文件夹执行该脚本,从我的Python代码内。

+1

的第一点[关于如何在StackOverflow上提出问题的指导原则](http://stackoverflow.com/help/how-to-ask)说,你应该寻找和研究。在你问这个问题之前,你到目前为止还在尝试搜索/研究什么? – 2014-12-02 08:57:16

回答

3

os模块允许你运行自定义脚本这样

import os 

os.system("dot -Tpdf somefile.dot -o somefile.pdf") 

你可以找到更多信息here

0

更好地使用subprocess.Popen将有跟踪误差的输出:

import subprocess 
child = subprocess.Popen(["dot -Tpdf somefile.dot -o somefile.pdf"],stdout=subprocess.PIPE, shell=True) 
output,err = child.communicate()