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)
Rainer
  • 19
  • 1
  • 8
  • You may need to add `pause` to the script or execute with `/k` – Sasha Jun 05 '18 at 12:25
  • @Jaxi Can you give me an example? – Rainer Jun 05 '18 at 12:28
  • see hadi's answer. That should be what you want. – Sasha Jun 05 '18 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. – zwer Jun 05 '18 at 13:53
  • @zwer I think your answer works. I added (') before and after the (") and now i don't have "access" "WinError5" – Rainer Jun 05 '18 at 14:09
  • @Rainer - Don't add any additional quotes as that's what the `subprocess` module does for you - you're getting that error, most likely, due to it not recognizing `'cmd` (notice the apostrophe) as an executable. – zwer Jun 05 '18 at 14:15
  • @zwer I added them because it didn't work without them. Do I only have to change the path or something else? – Rainer Jun 05 '18 at 14:21
  • What does it print when you do: `print(subprocess.check_output("C:\\CONVERTER\\MFD2MAT\\convert.cmd", stderr=subprocess.STDOUT, shell=True))` ? – zwer Jun 05 '18 at 14:27
  • @zwer I think i figured out what happens. The cmd file is executed correctly but the converter I use doesn't work with the anaconda-prompt (which I think is used here). The converter says that it couldnt find the file to convert. When i execute the cmd file with the window from windows it works. Is it possible to execute my cmd file with the windows-cmd window? – Rainer Jun 05 '18 at 14:40
  • 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)` – zwer Jun 05 '18 at 15:05
  • @zwer Thank you very much! The second one is working. The converter couldn't find the file I want to convert with the first one. But with the second one it works just like I wanted it to. Thank you! – Rainer Jun 06 '18 at 06:19

2 Answers2

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.

Hadi Farah
  • 1,091
  • 2
  • 9
  • 27
  • Do I have to put the path which works when i manually execute the cmd file with the command window in between the (") symbols? – Rainer Jun 05 '18 at 12:44
  • 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. – Hadi Farah Jun 05 '18 at 12:46
  • This gives me the following error message: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape – Rainer Jun 05 '18 at 12:48
  • @Rainer before your cmd path string put r, so for example `r'cmd.exe'` – Sasha Jun 05 '18 at 12:51
  • did you remember to escape your path front slashes ? Z.B: C:\Users\praktikant3\ would become C:\\Users\\praktikant3\\ – Hadi Farah Jun 05 '18 at 12:52
  • @Jaxi The r before the path removed the error. But the .cmd file doesn't open. – Rainer Jun 05 '18 at 13:01
  • @HadiFarah I got the path by pulling the .cmd file inside of the command window. After that i copied the path which the command window showed but without the (") in the beginning and end and pasted it in between the (") of your exampe – Rainer Jun 05 '18 at 13:04
  • add `\n` at the end of the command string: `commands = "call C:\\Users\\praktikant3\\TESTING.cmd\n"` Call is not really necessary I was just messing around. This works on my end after testing it. – Hadi Farah Jun 05 '18 at 13:06
  • @HadiFarah I can't get it to work properly. I updated my current code in the question above. Maybe you could check it. It doesn't execute the .cmd file. – Rainer Jun 05 '18 at 13:30
  • remove the r, it stopped working when I added r before my string – Hadi Farah Jun 05 '18 at 13:39
  • I have edited my answer to be clearer, let me know if it helps and use the same format don't add anything. And maybe one method is better than the other you can try both. – Hadi Farah Jun 05 '18 at 13:43
  • @HadiFarah Now i get an error from the subprocess.py: line 1113, in _communicate stdout = stdout[0] Index out of range – Rainer Jun 05 '18 at 13:48
  • Thank you for your help @HadiFarah. In the end the solution from "zwer" did the job. Maybe because the converter I use is a bit picky. – Rainer Jun 06 '18 at 06:25
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)
Rainer
  • 19
  • 1
  • 8