I am trying to write a script that opens a text file that contains specific lines which can be entered into the command window. Instead of having the user copy and paste these lines, I am trying to use subprocess to automatically make the commands run. Currently my code is this:
import sys
from Tkinter import *
import tkFileDialog
import subprocess
master = Tk()
master.withdraw()
file_path = tkFileDialog.askopenfilename(title="Open command file")
print file_path
with open (file_path,"r") as infile:
cmd1 = infile.readline()
cmd2 = infile.readline()
cmd3 = infile.readline()
cmd4 = infile.readline()
p=subprocess.Popen("{}; {}; {}; {}".format(cmd1, cmd2, cmd3, cmd4), stdout=subprocess.PIPE)
for line in p.stdout:
print line
p.wait()
print p.returncode
print 'complete'
The text file that the user selects has four commands which are all similar to the following:
program.exe commanduniquetoprogram "C:\\...." "C:\\...."
I don't know how the command line works, but if run in a command window, it does as it should without even opening the program. As it is now, when I run my program the only thing that seems to work is the first part program.exe
as it opens up the program then throws a strange error. Instead, if the line is pasted into the command window, the program does not open up at all and does its job, which leads me to believe that subprocess doesn't like the spaces in the command line.
Any help is appreciated as I have no experience with the subprocess module!