【问题标题】:Python script waits external command to be finished before continuingPython脚本在继续之前等待外部命令完成
【发布时间】:2018-04-11 16:36:04
【问题描述】:

我有一个非常简单的 Python 脚本,它解析一些数据并将输出写入文本文件。

with open(debug, 'w') as debugFile:
 debugFile.write(metreDebug)

在脚本的最后,我想打开这个文本文件,以便用户可以直接看到输出。

osCommandString = "notepad.exe " + debug
os.system(osCommandString)

是的...这是 Windows 上的开发。 不幸的是,python 脚本在继续之前等待用户关闭记事本。

关于如何解决这个问题的任何想法?

感谢您的帮助。

【问题讨论】:

  • 如果你真的要使用os.system,你必须使用start notepad.exe。但是你真的不应该使用 os.system,正如它自己的文档所解释的那样。

标签: python windows command external


【解决方案1】:

改用子流程模块。

import subprocess
with open(debug, 'w') as debugFile:
 debugFile.write(metreDebug)

osCommandString = "notepad.exe " + debug
subprocess.Popen(osCommandString)

您可以在此处阅读更多信息:

https://docs.python.org/2/library/subprocess.html

【讨论】:

    【解决方案2】:

    谢谢! 我正在使用

    subprocess.call(osCommandString)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      相关资源
      最近更新 更多