1

I have a .cmd file and I want to open it from a python script. The .cmd file (a converter) does its job when I open it without any further interaction needed in the command window. This means I only have to open it from my script, and that's it.

I tried the following...

from subprocess import check_output

def convert():
    subprocess.Popen(['[path to the .cmd file]')

... but it only opens the cmd window for a fraction of a second, and the actual .cmd file I want to run is not executed. What do I have to change to open the .cmd file behind my path?

UPDATE

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)
11
  • You may need to add pause to the script or execute with /k Commented Jun 5, 2018 at 12:25
  • @Jaxi Can you give me an example? Commented Jun 5, 2018 at 12:28
  • see hadi's answer. That should be what you want. Commented Jun 5, 2018 at 12:44
  • subprocess.check_output(["cmd", "/c", "absolute\\path\\to\\your\\script.cmd"]) should work fine. In fact, even just calling the file should work, I'd print the output to see what's going on. Commented Jun 5, 2018 at 13:53
  • @zwer I think your answer works. I added (') before and after the (") and now i don't have "access" "WinError5" Commented Jun 5, 2018 at 14:09

2 Answers 2

1

I will try to re-iterate with explanation:

First Method:

from subprocess import Popen, PIPE 

process = Popen("cmd.exe", shell=False, universal_newlines=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)


commands = "C:\\Users\\praktikant3\\TESTING.cmd\n" #you can use " " for 1 line of commands or '''  ''' for several lines
out, err = process.communicate(commands)
print(out)

While formulating your command, keep in mind that your command in the form of a python string, therefore:

  • Make sure to escape your front/backslashes in your path: C:\.. => C:\\..
  • Add \n to the end of the string to signal a newline to execute your command.

In my code, you can only see the output if you use print(out), however the cmd file runs regardless.


Second Method:

import sys
import os
def run_command(command):
    print("Running command: {}".format(command))
    os.system(command)

commands = "C:\\Users\\praktikant3\\TESTING.cmd"
run_command(commands)

This is neat if you are using only one line of commands, and you don't need the break line \n, os.system does that for you. Also os.system will display the output in your IDE without the need to print anything.

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

13 Comments

Do I have to put the path which works when i manually execute the cmd file with the command window in between the (") symbols?
yes, unless the .cmd file is in the same current working directory as your python script. But when I use subprocess I tend to always give the path to my files regardless just to be sure.
This gives me the following error message: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
@Rainer before your cmd path string put r, so for example r'cmd.exe'
did you remember to escape your path front slashes ? Z.B: C:\Users\praktikant3\ would become C:\\Users\\praktikant3\\
|
0

My cmd file doesn't work with the "Anaconda-Prompt"

Solution (from @zwer) If that's the issue, you can execute the command in a sub-process of a sub-process as:

subprocess.call(["cmd", "/k", "start", "", "C:\\CONVERTER\\MFD2MAT\\convert.cmd"], stderr=subprocess.STDOUT) 

but if it's failing due to the path you might just want to supply a cwd argument and be done with it, i.e.:

 subprocess.call("convert.cmd", cwd="C:\\CONVERTER\\MFD2MAT", stderr=subprocess.STDOUT, 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.