Having trouble with file modes

E

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_defines.php', 'w+')
data = f.read()
#patterns removed for brevity.
patterns = [(tuples)(blah blah)]

#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 modes I've tried either
produce an empty file or append the data onto the end of the file. How
Can I:

Open
Read
Truncate
....process data....
Write
Close

Do I need to open and close the file twice? (once in 'r' and then again
in 'w')

Thanks!
Erik
 
M

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 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 from
http://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
 
E

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!
 
L

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
 
H

Hendrik van Rooyen

#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 modes I've tried either
produce an empty file or append the data onto the end of the file. How

I would take a hard look at what the find and replace does - it acts as if you
are replacing
*everything* with *nothing*....

- Hendrik
 
D

Dennis Lee Bieber

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).
"w+" implies opening the file AS IF WITH "w" (ie, create a new file,
or truncate an existing file), and then allow seek() and read() on it.

"r+" implies opening an existing file as if with "r", and then
applying seek() and write() on it.

If you are using variable length records, the best process is to
open "r" the existing file, open "w" a temporary output file, process
r->w, close & delete the original, rename the temporary to the original
name.

With fixed length records, the use of a seek() between read and
write operations allows for updates in place (though deleting a record
requires moving all subsequent records up to fill the gap).
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
F

Fredrik Lundh

erikcw said:
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:
''

the standard approach when updating text files is to create a *new*
file, though:

fi = open(infile)
fo = open(infile + ".tmp", "w")

filter fi to fo

fo.close()
fi.close()

if os.path.exists(infile + ".bak")
os.remove(infile + ".bak")
os.rename(infile, infile + ".bak")
os.rename(infile + ".tmp", infile)

this leaves the old file around with a .bak extension, which is nice
if something goes wrong during filtering.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top