0

I want to make a script which opens a command prompt window and input commands into that prompt without any user interaction. I have been using the subprocess module with little success. What I have so far:

def subprocess_cmd(command):
    process = Popen(command,stdout=PIPE, shell=True)
    proc_stdout = process.communicate()[0].strip()
    print proc_stdout


subprocess_cmd('"C:\system\cmd.exe" & C:\dir\mybat.bat & C:\dir\gdal_translate C:\dir2\mypdf.pdf C:\dir\mytif.tif')

Now it runs through without error, but nothing happens. There should be a .tif file in the dir2 folder but as I said, nothing appears. When I run through the command prompt myself, it works fine.

Will B
  • 387
  • 3
  • 6
  • 14
  • Instead of trying to run it in one line, why don't you create a batch file using python and then run that batch file through cmd.exe? – Monacraft Apr 16 '15 at 01:15
  • I'm a bit rusty on Windows shell stuff, but does that first cmd.exe actually do what you intend? I think it will launch a new shell, and it would never get to the commands after the & until you manually exit the shell. – Eric Renouf Apr 16 '15 at 01:17

1 Answers1

1

I think the problem is you are not calling the methods and constants from the subprocess class. This worked for me in Python 3:

import subprocess

def subprocess_cmd(command,c="C:\\Users\\Alex"):
    process = subprocess.Popen(command,stdout=subprocess.PIPE,shell=True,cwd=c)
    proc_stdout = process.communicate()[0].strip()
    print(proc_stdout)

>>> subprocess_cmd('"cmd.exe" && "C:\\Users\\Alex\\test.bat"','C:\\Users\\Alex\\')
b'Microsoft Windows [Version 6.3.9600]\r\n(c) 2013 Microsoft Corporation. All rights reserved.\r\n\r\nC:\\Users\\Alex>\r\nC:\\Users\\Alex>mkdir thisisanewdirectory'

>>> subprocess_cmd('test.bat')
b'C:\\Users\\Alex>mkdir thisisanewdirectory'
Alex W
  • 37,233
  • 13
  • 109
  • 109
  • I just made it easier on myself by using: from subprocess import Popen. I tried it the way you have written and it still doesn't work. – Will B Apr 16 '15 at 01:27
  • The thing is the C:\dir\mybat.bat seems to be printing to the Python Interpreter, so it is recognizing it. – Will B Apr 16 '15 at 01:29
  • @WillB Maybe the subprocess's working directory is currently set to where the Python executable is? [Is this relevant?](http://stackoverflow.com/questions/21406887/python-subprocess-changing-directory) Also, you have only one command wrapped in quotes could that be part of the issue? – Alex W Apr 16 '15 at 01:48
  • The problem seems to be with `cmd`. I think [this comment](http://stackoverflow.com/questions/8055371/how-to-run-two-commands-in-one-line-in-windows-cmd#comment-34854597) explains what you're seeing. And you may have to run those commands separately. – Alex W Apr 16 '15 at 02:01
  • So you're saying I should run the (& [...] command1 & command2) line, but seperately. Could you explain a bit further if you have the time? Thanks for looking into it. – Will B Apr 16 '15 at 02:17
  • Yep. I added the working code. Basically, there's no need to pass the batch file to `cmd` because you can just execute it standalone. However, you can do both as shown above. The key was to pass the current working directory to Popen because it was executing the batch file in the `C:\Python34` directory otherwise. – Alex W Apr 16 '15 at 02:45
  • So I have run a smilar code which is subprocess_cmd(' "cmd.exe" && "C:\dir\mybat.bat" & C:\dir2\gdal_translate C:\dir3\mypdf.pdf C:\dir3\test.tif') Unfortunately, there has still been no created tif file in my desired folder. There shouldn't be a syntax issue seeing as it works when I run it myself through the prompt. – Will B Apr 16 '15 at 03:03
  • I would guess that the problem is that you are not escaping your backslashes (use \\ instead of \ ). – Alex W Apr 16 '15 at 03:06
  • You're a genius, you figured it out. Thank you very much! – Will B Apr 16 '15 at 03:08
  • Haha! You're welcome. I'm always up for dabbling with Python. – Alex W Apr 16 '15 at 03:12