Re: Having trouble with file modes



To make it write over the data, I ended up adding, which seems to work
fine.

f = open('_i_defines.php', 'w+')
data = f.read()
f.seek(0)
f.truncate(0)
....process data, write file, close...

Now that I look at it, I could probably take out the f.seek().

Thanks for your help!


On Nov 3, 4:00 pm, "martdi" <martin.d...@xxxxxxxxx> wrote:
At first I was convinced that "w+" was the tool for the job. But now
I'm finding that the contents of the file are deleted (so I can't read
the data in).Possible File Modes:
a : Append -- Add data at the end of the file
r : Read -- Read from the file
w : write -- Flush contents of the file and put data in it
r+ : read and write -- Make changes to the file
any of the above modes with b added at the end of the mode sets the
file in binary mode

you might want to check section 7.2 fromhttp://docs.python.org/tut/node9.html

Ofen when doing file operations you might prefer to read the file to
modify, make changes in memory then save back in an other file, so you
can make sure the changes are ok before you replace the old one

.