Re: Having trouble with file modes

2006-11-03 Thread Fredrik Lundh
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

Re: Having trouble with file modes

2006-11-03 Thread Hendrik van Rooyen
"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

Re: Having trouble with file modes

2006-11-03 Thread Larry Bates
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

Re: Having trouble with file modes

2006-11-03 Thread erikcw
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

Re: Having trouble with file modes

2006-11-03 Thread martdi
> 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

Having trouble with file modes

2006-11-03 Thread erikcw
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