I am trying to read from an initially empty file after writing before closing it. Is this possible in Python?
some_data = "this is a string"
with open("outfile1.txt", 'r+') as f:
print "Writing ..."
f.write(some_data)
f.flush()
print "File contents:"
print f.read()
print "Again ..."
print open("outfile1.txt", 'r').read()
Flushing with help f.flush()does not seem to work, since the latter print f.read()does not print anything.
Is there a way to read the "current data" from a file other than reopening it?
source
share