<div>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.</div>
<br><div class="gmail_quote">On Wed, Jun 17, 2009 at 2:09 PM, Jean-Michel Pichavant <span dir="ltr"><<a href="mailto:jeanmichel@sequans.com">jeanmichel@sequans.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">Cameron Pulsford wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hey all, hopefully a simple question.<br>
<br>
I'm writing a simple python tool that opens a file, and does something like<br>
<br>
for line in file.readlines():<br>
    temp.write(line.doStuff())<br>
<br>
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? <br>

Thanks in advance<br>
<br>
</blockquote>
<br></div></div>
Altering directly the file is dangerous, what if something goes wrong during the process ?<br>
Create a temp file and copying it if successful is your best bet.<br>
<br>
I guess using python modules like tempfile and shutil are  a pythonic way to do it :<br>
<br>
import tempfile<br>
import shutil<br>
<br>
In [14]: tempfile.NamedTemporaryFile?<br>
Definition:     tempfile.NamedTemporaryFile(mode='w+b', bufsize=-1, suffix='', prefix='tmp', dir=None)<br>
Docstring:<br>
   Create and return a temporary file.<br>
   Arguments:<br>
   'prefix', 'suffix', 'dir' -- as for mkstemp.<br>
   'mode' -- the mode argument to os.fdopen (default "w+b").<br>
   'bufsize' -- the buffer size argument to os.fdopen (default -1).<br>
   The file is created as mkstemp() would do it.<br>
<br>
   Returns an object with a file-like interface; the name of the file<br>
   is accessible as <a href="http://file.name" target="_blank">file.name</a>.  The file will be automatically deleted<br>
   when it is closed.<br>
<br>
<br>
In [7]: shutil.copy?<br>
Definition:     shutil.copy(src, dst)<br>
Docstring:<br>
   Copy data and mode bits ("cp src dst").<br>
<br>
   The destination may be a directory.<br>
<br>
<br>
</blockquote></div><br>