Friday, 15 August 2014

Quick write to a file for a Python subprocess -



Quick write to a file for a Python subprocess -

i have shell programme needs pointed @ text file. write temporary file, run programme on via python's subprocess method, , save output. here's have far:

with open('test.txt', 'w') f: in range(50): f.write('sometext %d'%i) f.flush() output = subprocess.check_output('./program test.txt', shell=true, stderr=subprocess.stdout)

output gets updated once. i'm assuming flush isn't able write file before next iteration of loop executes. other ways implemented? opening , closing file seems expensive, because code going called many times.

if want omit closing , re-opening (which cleanest way), might want f.seek(0) @ start , f.truncate() after writing.

otherwise, info appended file on every loop run , external programme might confused.

python file-io

No comments:

Post a Comment