Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8575

Re: Safely modify a file in place -- am I doing it right?

From Grant Edwards <invalid@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: Safely modify a file in place -- am I doing it right?
Date 2011-06-29 19:05 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <iuft14$du3$1@reader1.panix.com> (permalink)
References <4e0b6383$0$29996$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


On 2011-06-29, steve+comp.lang.python@pearwood.info <steve+comp.lang.python@pearwood.info> wrote:
> I have a script running under Python 2.5 that needs to modify files in
> place. I want to do this with some level of assurance that I won't lose
> data. E.g. this is not safe:
>
> def unsafe_modify(filename):
>     fp = open(filename, 'r')
>     data = modify(fp.read())
>     fp.close()
>     fp = open(filename, 'w')  # <== original data lost here
>     fp.write(fp)
>     fp.close()  # <== new data not saved until here
>
> If something goes wrong writing the new data, I've lost the previous
> contents.
>
> I have come up with this approach:
>
> import os, tempfile
> def safe_modify(filename):
>     fp = open(filename, 'r')
>     data = modify(fp.read())
>     fp.close()
>     # Use a temporary file.
>     loc = os.path.dirname(filename)
>     fd, tmpname = tempfile.mkstemp(dir=loc, text=True)
>     # In my real code, I need a proper Python file object, 
>     # not just a file descriptor.
>     outfile = os.fdopen(fd, 'w')
>     outfile.write(data)
>     outfile.close()
>     # Move the temp file over the original.
>     os.rename(tmpname, filename)
>
> os.rename is an atomic operation, at least under Linux and Mac, so if
> the move fails, the original file should be untouched.
>
> This seems to work for me, but is this the right way to do it?

That's how Unix programs have modified files "in place" since time
immemorial.

> Is there a better/safer way?

Many programs rename the original file with a "backup" suffix (a tilde
is popular).

-- 
Grant Edwards               grant.b.edwards        Yow! It's NO USE ... I've
                                  at               gone to "CLUB MED"!!
                              gmail.com            

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Safely modify a file in place -- am I doing it right? steve+comp.lang.python@pearwood.info - 2011-06-30 03:40 +1000
  Re: Safely modify a file in place -- am I doing it right? Grant Edwards <invalid@invalid.invalid> - 2011-06-29 19:05 +0000
  Re: Safely modify a file in place -- am I doing it right? Chris Torek <nospam@torek.net> - 2011-06-29 20:31 +0000
  Re: Safely modify a file in place -- am I doing it right? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-01 14:11 +1000

csiph-web