erikcw wrote:
> To make it write over the data, I ended up adding, which seems to work
> fine.
>
> f = open('_i_defines.php', 'w+')
that doesn't work; you need to use "r+" if you want to keep the original
contents.
"w+" means truncate first, update then:
>>> f = open("foo.txt", "r")
>>> f.r
"erikcw" <[EMAIL PROTECTED]> wrote:
8<
> #loop through patterns list and find/replace data
> for o, r in patterns:
> data = data.replace(o, r)
> print "Replaced %s with %s" % (o, r)
> f.write(data)
> f.close()
>
> This results in an empty file. All of the
Don't do that. Do something like renaming the old file
to .bak (or .aside or something) and then create the entire file
by opening it with 'w'.
-Larry
--
http://mail.python.org/mailman/listinfo/python-list
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, "m
> 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 fi
Hi all,
I've created a script that reads in a file, replaces some data (regex),
then writes the new data back to the file.
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).
f = open('_i_defin