在Python中执行类似命令的命令

2024-06-26 17:39:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个Python代码,它将像cmd那样打开一个程序,然后从文件菜单导出一个.txt文件。cmd的代码如下:

c:\ESG\Statsvis.exe \\192.168.100.222\c\ESG\S1-424\2012\06\29\S1-42420120629.dsf /output=C:\Users\jessica.macleod\Desktop\outfile.txt /param=RMS Amplitude

在命令行中,上面的行正是我想要的。Python的等价物是什么?


Tags: 文件代码程序txtcmdoutput菜单exe
3条回答

如果希望具有精确的shell/cmd行为,那么在一个suprocess.Popen()调用中将shell参数设置为True。但是,从文件来看:

Warning

Invoking the system shell with shell=True can be a security hazard if combined with untrusted input. See the warning under Frequently Used Arguments for details.

请参见subprocess.Popen,如下所示:

subprocess.Popen(["/bin/ls", "-l"]

或者,根据您希望得到的结果(stdout,返回代码),使用本模块中的subprocess.callsubprocess.call_check或其他片段。

另一种方法是os.system()

import os
os.system("c:\\ESG\\Statsvis.exe \\192.16...0629.dsf /output=C:\\...\\outfile.txt ...")

相关问题 更多 >