Pythonic way to overwrite a file
Jean-Michel Pichavant
jeanmichel at sequans.com
Wed Jun 17 14:09:04 EDT 2009
Cameron Pulsford wrote:
> Hey all, hopefully a simple question.
>
> I'm writing a simple python tool that opens a file, and does something
> like
>
> for line in file.readlines():
> temp.write(line.doStuff())
>
> However, I want to provide the option do this "in place", as in have
> the destination file be the same as the source file. Currently, I am
> writing to a temp file and then using "os.system('mv %s %s' %
> (dstfile, srcfile))" to copy the destination file onto the soruce
> file. This is extremely ugly though, and will only work on unix based
> systems (I'm guessing, unless windows has mv too). Is there a more
> pythonic way to do this? Ideally I'd like to change the file as I go
> through it and not deal with a second file at all. That wouldn't have
> any atomicity though... What would be the most pythonic+safest way to
> do this?
>
> Thanks in advance
>
Altering directly the file is dangerous, what if something goes wrong
during the process ?
Create a temp file and copying it if successful is your best bet.
I guess using python modules like tempfile and shutil are a pythonic
way to do it :
import tempfile
import shutil
In [14]: tempfile.NamedTemporaryFile?
Definition: tempfile.NamedTemporaryFile(mode='w+b', bufsize=-1,
suffix='', prefix='tmp', dir=None)
Docstring:
Create and return a temporary file.
Arguments:
'prefix', 'suffix', 'dir' -- as for mkstemp.
'mode' -- the mode argument to os.fdopen (default "w+b").
'bufsize' -- the buffer size argument to os.fdopen (default -1).
The file is created as mkstemp() would do it.
Returns an object with a file-like interface; the name of the file
is accessible as file.name. The file will be automatically deleted
when it is closed.
In [7]: shutil.copy?
Definition: shutil.copy(src, dst)
Docstring:
Copy data and mode bits ("cp src dst").
The destination may be a directory.
More information about the Python-list
mailing list