I have a shell program that needs to be pointed at a text file. I would like to write to a temporary file, run this program on it via Python's subprocess method, and save the output. Here's what I have so far:
with open('test.txt', 'w') as f:
for i in range(50):
f.write('sometext %d'%i)
f.flush()
output = subprocess.check_output('./program test.txt', shell=True, stderr=subprocess.STDOUT)
Output only gets updated once. I'm assuming that flush
isn't able to write to the file before the next iteration of the loop executes. What other ways could this be implemented? Opening and closing the file seems like it would be expensive, because this code is going to be called many times.