After reading some messages, it seems that you can open the file for reading and writing in "r +" or "w +" mode. However, attempts to use these modes always give me strange results:
- If I use 'r +', call file.read () and then call file.write ('str'), there will be an error "IOError: [Errno 0] Error"
- If I use 'r +', call file.write ('str') and then call file.read (), it will return unexpected and very long content (looks like inside an object)
- If I use 'w +', calling file.read () will return an empty string
What I'm trying to do is open a file, read the contents, modify it and write. Currently, I open it with "r", change the content and open it again with "w" and write it back. Is this a good way to do this?
Example: http://snipt.org/zglJ0
I am using window 7 and python 2.7.2
source
share