0

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-424\2012\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

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi defuz, I looked into this particular command, but I'm still not quite sure how it works. How does that call the command I have above? edit: by that, I mean the /output:
try something like this: ["c:\\ESG\\Statsvis.exe", "\\\\192.16...29.dsf", "/output=C:\\Users\\jessica.macleod\\Desktop\\outfile.txt", "/param=RMS", "Amplitude"]
I have tried using the subprocess.call() command to open the program, but I'm still not quite sure how to tell it to execute the command. What would be the proper way to write the output path and the "param" that I want Python to execute?
Every \ must be escaped (replace to \\), and command line must be split to list by whitespace
1

Another way would be os.system().

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

1 Comment

Backslashes in path will not be correctly interpreted in this example.
0

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

0

If you need the output of the command use subprocess:

import subprocess
out = subprocess.check_output("dir c:\ /AD", shell = True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.