read and write the same text file

I

iMath

read and write the same text file
Open a text file ,read the content ,then make some change on it ,then writeit back to the file ,now the modified text should only has the modified content but not the initial content ,so can we implement this by only set the mode parameter with open() function ?if yes ,what the parameter should be?if no ,can we implement this by only one with statement ?
I implement this with 2 with statement as the following

replace_pattern = re.compile(r"<.+?>",re.DOTALL)

def text_process(file):

with open(file,'r') as f:
text = f.read()

with open(file,'w') as f:
f.write(replace_pattern.sub('',text))
 
R

Roy Smith

iMath said:
read and write the same text file
Open a text file ,read the content ,then make some change on it ,then write
it back to the file ,now the modified text should only has the modified
content but not the initial content ,so can we implement this by only set the
mode parameter with open() function ?if yes ,what the parameter should be ?if
no ,can we implement this by only one with statement ?
I implement this with 2 with statement as the following

replace_pattern = re.compile(r"<.+?>",re.DOTALL)

def text_process(file):

with open(file,'r') as f:
text = f.read()

with open(file,'w') as f:
f.write(replace_pattern.sub('',text))

At a minimum, you need to close the file after you read it and before
you re-open it for writing.

There's a variety of ways you could achieve the same effect. You might
open the file once, in read-write mode, read the contents, rewind to the
beginning with seek(), then write the new contents. You might also
write the modified data out to a new file, close it, and then rename it.
But, open, read, close, open, write, close is the most straight-forward.
 
S

Steven D'Aprano

At a minimum, you need to close the file after you read it and before
you re-open it for writing.

The "with" statement automatically does that. When the indented block --
in this case, a single line "text = f.read()" -- completes, f.close() is
automatically called.

Technically, this is a property of file objects, not the with statement.
The with statement merely calls the magic __exit__ method, which for file
objects closes the file.

There's a variety of ways you could achieve the same effect. You might
open the file once, in read-write mode, read the contents, rewind to the
beginning with seek(), then write the new contents. You might also
write the modified data out to a new file, close it, and then rename it.
But, open, read, close, open, write, close is the most straight-forward.

All of those techniques are acceptable for quick and dirty scripts. But
for professional applications, you want something which is resistant to
data loss. The problems happens when you do this:

1. Read file.
2. Modify data.
3. Open file for writing. # This deletes contents of the file!
4. Write data.
5. Flush data to the hard drive.

Notice that there is a brief time frame where the old data is destroyed,
but the new data hasn't been saved to the hard drive yet. This is where
you can get data loss if, say, the power goes off.

So a professional-quality application should do something like this:

1. Read file.
2. Modify data.
3. Open a new file for writing.
4. Write data to this new file.
5. Flush data to the hard drive.
6. Atomically replace the original file with the new file.

Since sensible operating systems promise to be able to replace as a
single atomic step, guaranteed to either fully succeed or not occur at
all, this reduces the risk of data corruption or data loss significantly.

Even if you're using an OS that doesn't guarantee atomic replacement
*cough* Windows *cough* it still decreases the risk of data loss, just
not as much as we would like.

But having said all that, for "normal" use, a simple read-write cycle is
still pretty safe, and it's *much* simpler to get right.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top