Re: Having trouble with file modes
- From: "erikcw" <erikwickstrom@xxxxxxxxx>
- Date: 3 Nov 2006 13:35:05 -0800
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 nowa : Append -- Add data at the end of the file
I'm finding that the contents of the file are deleted (so I can't read
the data in).Possible File Modes:
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
.
- Follow-Ups:
- Re: Having trouble with file modes
- From: Fredrik Lundh
- Re: Having trouble with file modes
- References:
- Having trouble with file modes
- From: erikcw
- Re: Having trouble with file modes
- From: martdi
- Having trouble with file modes
- Prev by Date: Re: Programming Language that is Spread***/Table Based
- Next by Date: Re: Having trouble with file modes
- Previous by thread: Re: Having trouble with file modes
- Next by thread: Re: Having trouble with file modes
- Index(es):