read and write the same text file

Discussion in 'Python' started by iMath, Mar 9, 2013.

  1. iMath

    iMath Guest

    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))
    iMath, Mar 9, 2013
    #1
    1. Advertising

  2. iMath

    Roy Smith Guest

    In article <>,
    iMath <> wrote:

    > 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.
    Roy Smith, Mar 9, 2013
    #2
    1. Advertising

  3. On Sat, 09 Mar 2013 10:47:34 -0500, Roy Smith wrote:

    > In article <>,
    > iMath <> wrote:


    >> 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.


    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.



    --
    Steven
    Steven D'Aprano, Mar 10, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabor
    Replies:
    19
    Views:
    1,107
    Steve Holden
    May 31, 2005
  2. Jp Calderone
    Replies:
    7
    Views:
    1,173
    Mike Meyer
    May 28, 2005
  3. YUS
    Replies:
    2
    Views:
    364
  4. JonathanB

    Read and Write the same file

    JonathanB, May 2, 2007, in forum: Python
    Replies:
    5
    Views:
    282
    Grant Edwards
    May 2, 2007
  5. Chris
    Replies:
    3
    Views:
    240
    Steven D'Aprano
    Sep 11, 2007
Loading...

Share This Page