Pythonic way to overwrite a file

Cameron Pulsford cameron.pulsford at gmail.com
Wed Jun 17 14:26:07 EDT 2009


Essentially it just cleans up a source file of erroneous spaces and tabs and
can also convert tabs to spaces so loading the whole file into memory is
possibly an option. I am making this utility for personal use, and that
would definitely be fine, but if it turned out well I'd open source it and
then I wouldn't be so sure. This is only supposed to handle text files, so
when would reading it all into memory become too much for most computers?
I'm guessing there aren't many source files of too great a size.

On Wed, Jun 17, 2009 at 2:09 PM, Jean-Michel Pichavant <
jeanmichel at sequans.com> wrote:

> 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.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090617/df9aae61/attachment-0001.html>


More information about the Python-list mailing list