I want to make a Python code that will open a program like cmd would, then export a .txt file from the file menu. The code looks like this for cmd:
c:\ESG\Statsvis.exe \192円.168.100.222\c\ESG\S1-4242012円06円29円\S1-42420120629.dsf /output=C:\Users\jessica.macleod\Desktop\outfile.txt /param=RMS Amplitude
In cmd, the above line does exactly what I want. What would be the equivalent for Python?
4 Answers 4
See subprocess.Popen, like this:
subprocess.Popen(["/bin/ls", "-l"]
Or, depending on what you want to get as result (stdout, return code), use subprocess.call, subprocess.call_check, or other snippets in this module.
4 Comments
["c:\\ESG\\Statsvis.exe", "\\\192円.16...29.dsf", "/output=C:\\Users\\jessica.macleod\\Desktop\\outfile.txt", "/param=RMS", "Amplitude"]Another way would be os.system().
import os
os.system("c:\\ESG\\Statsvis.exe \192円.16...0629.dsf /output=C:\\...\\outfile.txt ...")
1 Comment
If you want to have exact shell/cmd behavior, then set the shell argument to True in a suprocess.Popen() call. However, from the documentation:
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.
Comments
If you need the output of the command use subprocess:
import subprocess
out = subprocess.check_output("dir c:\ /AD", shell = True)