Reputation:
I have a code which I am writing 'testing' into, once the file is done I was hoping to see 'testing' in the text file but it is still empty. Am I missing something here?
import shutil
import sys
f = open('test.txt', 'r+')
f.write('testing')
shutil.copyfileobj(f, sys.stdout)
Upvotes: 1
Views: 61
Reputation: 29794
It is actually correct, the problem is that when you write
, the buffer pointer moves, so when you copy, it won't print anything. Try with seek
before like this:
import shutil
import sys
f = open('test.txt', 'r+')
f.write('testing')
f.seek(0)
shutil.copyfileobj(f, sys.stdout)
Hope this helps!
Upvotes: 4
Reputation: 8685
you need to close the file.
f.close()
EDIT
try changing the name of the file, Is it still not writting:
f = open('test124.txt', 'a') # use the append flag so that it creates the file.
f.write('testing')
f.close()
Upvotes: 3