Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
interaction of mode 'r+', file.write(), and file.tell(): a bug orundefined behavior?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Anthony Tolle, post: 4017415"] My guess is that there are a few possible problems: 1) In this case, writing to file opened with 'r+' without an explicit f.seek is probably not a good idea. The file iterator (for line in f) uses a readahead buffer, which means you can't guarantee what the current file position will be. 2) It may be necessary to do an explicit f.flush or f.close when writing to an 'r+' file. In your case, the close should automatically happen when the f object falls out of scope, which tells me that were still looking at some other problem, like not using f.seek 3) It is possible that f.tell implicitly flushes buffers used by the file object. That would explain why uncommenting the f.tell causes the writes to show up. What are you trying to accomplish? Overwrite the original file, or append to it? If you want to overwrite the file, it may be better to generate a new file, delete the old one, then rename the new one. If you want to append, then it would be better to open the file with append mode ('a') [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
interaction of mode 'r+', file.write(), and file.tell(): a bug orundefined behavior?
Top