【发布时间】:2018-06-06 14:34:45
【问题描述】:
我有一个.cmd 文件,我想从 python 脚本打开它。 .cmd 文件(一个转换器)在我打开它而不需要在命令窗口中进行任何进一步的交互时完成它的工作。这意味着我只需要从我的脚本中打开它,就是这样。
我尝试了以下...
from subprocess import check_output
def convert():
subprocess.Popen(['[path to the .cmd file]')
... 但它只打开 cmd 窗口几分之一秒,并且我要运行的实际 .cmd 文件没有执行。要打开路径后面的 .cmd 文件,我需要进行哪些更改?
更新
from subprocess import Popen, PIPE
def convert():
process = Popen("cmd.exe", shell=False, universal_newlines=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
commands = r"C:\\CONVERTER\MFD2MAT\\convert.cmd\n"
out, err = process.communicate(commands)
【问题讨论】:
-
您可能需要在脚本中添加
pause或使用/k执行 -
@Jaxi 你能举个例子吗?
-
见哈迪的回答。这应该是你想要的。
-
subprocess.check_output(["cmd", "/c", "absolute\\path\\to\\your\\script.cmd"])应该可以正常工作。事实上,即使只是调用文件也应该可以工作,我会打印输出以查看发生了什么。 -
@zwer 我认为您的回答有效。我在(“)之前和之后添加了('),现在我没有“访问”“WinError5”
标签: python python-3.x