【问题标题】:Execute script from Python code [duplicate]从Python代码执行脚本[重复]
【发布时间】:2014-12-02 08:52:07
【问题描述】:

我使用 Python 生成了一个点文件。

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

这是脚本:

dot -Tpdf somefile.dot -o somefile.pdf

我想知道是否可以在我的 Python 代码中在当前文件夹中执行此脚本。

【问题讨论】:

标签: python bash shell


【解决方案1】:

os 模块允许您执行这样的自定义脚本

import os

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

您可以找到更多信息here

【讨论】:

    【解决方案2】:

    最好使用subprocess.Popen 将跟踪错误和输出:

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

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 1970-01-01
      • 2013-01-02
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2011-07-12
      • 2015-07-11
      相关资源
      最近更新 更多