[socal-piggies] weird behavior with files on different filesystems

Daniel Arbuckle djarb at highenergymagic.org
Fri Mar 7 12:17:47 PST 2008


Two more hints.

First, if you want a temp file, mkstemp isn't the function you want to
use. You really want tempfile.TemporaryFile or
tempfile.NamedTemporaryFile. mkstemp is a low-level interface, and
it'll just hurt you to use it. The code you posted doesn't actually
call mkstemp, but it's something to be aware of.

Second, there's absolutely no reason for you to be creating these temp
files at all, given the code you posted. Open the file for reading and
grab it's contents, just as you're doing now, then close it and open
it again for writing, write the header, write the original contents,
and close it.

On Fri, Mar 7, 2008 at 11:50 AM, William Yardley
<piggies at veggiechinese.net> wrote:
> I was working on a python script today, basically my first. It's just a
>  simple script to iterate through some files / directories and append a
>  comment with an SVN "Id" string to the files, and then set a property on
>  the file. I probably could have done it more quickly in perl / shell /
>  sed / whatever, but I wanted to play around with using python instead.
>
>  The filesystem my home directory (and the directory tree that I'm
>  modifying) are on is mounted via NFS. The filesystem that /tmp is on is
>  a local disk.
>
>  The program works fine *if* the source and destination file are on the
>  same filesystem (for example, if I do the svn co to /tmp and make the
>  temporary files in /tmp, or if I just name the temporary file
>  "filename.tmp").
>
>  However, if I make a unique temp file in /tmp using tempfile.mkstemp,
>  and then copy it into place, then some (but not all) of the resulting
>  files are 0 size, and many or all of the remaining ones are truncated
>  without a trailing newline. The behavior seems to be consistent (i.e.,
>  the same files are 0 size, filesizes of the files that are there are the
>  same if I re-checkout the tree and run the script again).
>
>  Earlier, I had some problems with closing files in the wrong place,
>  which was causing some similar problems with 0 size output files.
>
>  I put the program at:
>  http://veggiechinese.net/id_tags.txt
>
>  Any thoughts? And would it be better to read in the text files line by
>  line rather than sucking them into a string? None are super-huge.
>
>  Oddly, I just ran into a similar problem with a perl script that does
>  something kind of similar on a totally different machine.
>
>  w
>
>
>  _______________________________________________
>  socal-piggies mailing list
>  socal-piggies at lists.idyll.org
>  http://lists.idyll.org/listinfo/socal-piggies
>




More information about the socal-piggies mailing list