The with open('pf_d.txt', 'w+') as outputfile:
construct is called context manager. In this case, the resource is a file represented by the handle/file object outputfile
. The context manager makes sure that the file is closed when the context is left. Closing implicates flushing, and re-opening the file after that will show you all its contents. So, one option to solve your issue is to read your file after it has been closed:
with open('pf_d.txt', 'w+') as outputfile:
rc = subprocess.call(...)
with open('pf_d.txt', 'r') as outputfile:
print outputfile.readlines()
Another option is to re-use the same file object, after flushing and seeking:
with open('pf_d.txt', 'w+') as outputfile:
rc = subprocess.call(...)
outputfile.flush()
outputfile.seek(0)
print outputfile.readlines()
A file handle is always represented by a file pointer, indicating the current position in the file. write()
forwards this pointer to the end of the file. seek(0)
moves it back to the beginning, so that a subsequent read()
startes from the beginning of the file.