Created
September 10, 2013 22:04
-
-
Save notbenh/6516401 to your computer and use it in GitHub Desktop.
Trying to both append and read from a file handle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
f = open('somefile.txt','a+') | |
while True: | |
text = input('Say something silly ') | |
if text: | |
f.write(text) | |
print('CONTENT:',f.read()) | |
else: | |
f.close() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The updated version still isn't quite right. It will put the cursor
loc
bytes into the file, which is potentially backing it up in the file, ifsomefile.txt
already had contents in it.write()
returns the number of bytes written, not the absolute position in the file. If you wish to leave the cursor at the end of the file, you can dropf.seek(loc)
entirely.To see the problem I'm talking about try doing the following (untested, written in the textbox) code: