from subprocess import Popen, PIPE
import glob
import time
file_list=glob.glob(r'C:\Tony\files\*.txt')
numFiles = len(file_list)
p = Popen([r"C:\Tony\prog.bat"], stdin=PIPE)
start_time = time.time()
for x in range(0,numFiles):
command = "run {" + file_list[x] + "} "
p.communicate(input=command)[0]
print("--- %s seconds ---" % (time.time() - start_time))
I am starting a program by triggering the .bat
using Popen
. My program knows to take commands like run {path to .txt file}
and it does things.
This works fine for the first iteration. But then I get the following error:
Traceback (most recent call last):
File "run_tests.py", line 14, in <module>
p.communicate(input=command)[0]
File "c:\python27\lib\subprocess.py", line 465, in communicate
self.stdin.write(input)
ValueError: I/O operation on closed file
Why is the file getting closed?