file operations

Fredrik Lundh fredrik at pythonware.com
Wed Oct 31 14:49:28 EST 2001


"Kerstin" wrote:
> I think I managed most of my problems. It's not the best way I
> suppose, but it's working. I open the file in r-mode when I need to
> read it, close it, and open it again in w-mode to write it

if you're changing the size of the file, it's the usual way to
do it.  (especially if it's a text file, or any other file format
that wasn't designed to be modified in place)

most normal operating systems won't allow you to insert
or delete stuff inside a file.

> I tried the w+-mode, but it wasn't working properly for me.

"didn't work properly" as in "removed everything in my file"?

that's the expected behaviour, as mentioned in the library reference:

    Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+'
    truncates the file). Append 'b' to the mode to open the file in binary
    mode, on systems that differentiate between binary and text files
    (else it is ignored).

if you don't need to insert or delete bytes from the file, "r+" could work.

</F>





More information about the Python-list mailing list