0

I have some code here trying to open up the cmd.exe from Python and input some lines for the command to use.

Here it is:

PDF= "myPDF"
output= "my output TIF"

def my_subprocess(command,c='C:\here'):
     process = subprocess.Popen(command,stdout=subprocess.PIPE,shell=True,cwd=c)
     communicate = process.communicate()[0].strip()      

my_subprocess('"cmd.exe" && "C:\\here\\myinfamous.bat" && "C:\\my directory and lines telling cmd to do stuff"'+ PDF + " " + output)

When run with the rest of my script, the command prompt does not even open up and there seems to be no output or errors at all. My thought is that it has not even run the cmd.exe command so none of this code is going in to create the final output.

Is there something I am not doing properly?

Thank you.

zondo
  • 19,901
  • 8
  • 44
  • 83
Will B
  • 387
  • 3
  • 6
  • 14
  • Possible duplicate of [Python: Start new command prompt on Windows and wait for it finish/exit](http://stackoverflow.com/questions/11615455/python-start-new-command-prompt-on-windows-and-wait-for-it-finish-exit) – Cripto Mar 01 '16 at 03:13
  • Possible duplicate of [Running windows shell commands with python](http://stackoverflow.com/questions/14894993/running-windows-shell-commands-with-python) – ivan_pozdeev Mar 03 '16 at 23:25
  • Why do you think you need `cmd.exe` here? It looks like [XY problem](http://meta.stackexchange.com/a/66378/137096). What are you trying to do? Do you want to run a bat-file? Do you need to interact with it? Do you want to display a pdf file? Do you need to wait until the pdf document is closed? – jfs Mar 04 '16 at 13:05

1 Answers1

1

You need to replace subprocess.Popen with subprocess.call

Here is a working code on windows 8 that opens a text file using notepad. First field is the command itself and second field is argument.

You can modify these and test with your files.

import subprocess

subprocess.call(['C:\\Windows\\System32\\Notepad.exe', 'C:\\openThisfile.txt'])
Anil_M
  • 10,893
  • 6
  • 47
  • 74