I have a program that is normally run from the Windows Command Prompt. The program processes a single file at a time and creates an error log file. I would like to use Python to batch process multiple files in a folder, but I am having no luck at using Python to execute the program. I have tried the subprocess module (both subprocess.call and subprocess.Popen), but the program doesn't seem to run at all or at least doesn't produce the log file. I have also tried os.execv with limited success - the program executed and produced the error log, but then terminated the script after processing the first file with the error message "EOFError: [Errno 10054] An existing connection was forcibly closed by the remote host."
Can anyone point me in the right direction to get me started?
Thanks!
Mark
Solved! Go to Solution.
Got it working by creating an mpArgs variable and setting it equal to the string of arguments needed, with the first argument being the path and file name of the executable. Then the mpArgs variable is passed to subprocess.Popen(). Still not sure why the previous construction of the arguments directly in subprocess.Popen didn't work. Successful code below...
#Set variables for passing arguments to the mp program
mpPath = "pathToProgram"
workSpace = "folderContainingFilesToProcess"
#Create a list of files in the workspace and run them through mp
fileList = os.listdir(workSpace)
for file in fileList:
print "Processing " + file
mpArgs = os.path.join(mpPath, "mp.exe") + " -e " + os.path.join(workSpace, file[:-4]) + ".err " + os.path.join(workSpace, file)
subprocess.Popen(mpArgs)
As a starting point, please post your Python script.
Script variations I have tried so far...
mpPath = "pathToProgram"
workSpace = "folderContainingFilesToProcess"
fileList = os.listdir(workSpace)
for file in fileList:
print "Processing " + file
os.execv(os.path.join(mpPath, "mp"), ["mp", "-e " + os.path.join(workSpace, file[:-4]) + ".err", os.path.join(workSpace, file)])
I have also substituted the following for the os.execv line:
subprocess.Popen([os.path.join(mpPath, "mp.exe"), '-e ' + os.path.join(workSpace, "CemeteryOrBurialSite_P") + '.err', os.path.join(workSpace, "CemeteryOrBurialSite_P.xml")])
And I've tried subprocess.call and subprocess.check_call instead of Popen.
And I've tried taking it out of the For loop and hardcoding the file name and path.
Got it working by creating an mpArgs variable and setting it equal to the string of arguments needed, with the first argument being the path and file name of the executable. Then the mpArgs variable is passed to subprocess.Popen(). Still not sure why the previous construction of the arguments directly in subprocess.Popen didn't work. Successful code below...
#Set variables for passing arguments to the mp program
mpPath = "pathToProgram"
workSpace = "folderContainingFilesToProcess"
#Create a list of files in the workspace and run them through mp
fileList = os.listdir(workSpace)
for file in fileList:
print "Processing " + file
mpArgs = os.path.join(mpPath, "mp.exe") + " -e " + os.path.join(workSpace, file[:-4]) + ".err " + os.path.join(workSpace, file)
subprocess.Popen(mpArgs)