Temporary Files

Steve Holden sholden at holdenweb.com
Wed Apr 30 10:19:41 EDT 2003


"Tony Meyer" <ta-meyer at ihug.co.nz> wrote in message
news:mailman.1051682327.24987.python-list at python.org...
> [me]
> > Is there perhaps a different method of copying?  One that
> > doesn't take filenames, but file objects?
>
> Ack.  Yes, there is shutil.copyfileobj().
>
> *sigh*  Rephrased in light of this rather belated discovery; is this
> what I should be doing?
>
> >>> import shutil
> >>> import tempfile
> >>> out = tempfile.TemporaryFile("w")
> >>> out.write(data)
> >>> out.flush()
> >>> # shutil.copyfile(out.name, permanent_name)  # not this!
> >>> permanent_file = file(permanent_name)
> >>> shutil.copyfileobj(out, permanent_file)
> >>> out.close()
> >>> permanent_file.close()
>
> TIA.
>

Well, just to interrupt this internal dialog, would it help if you actually
*did* that and then tell us what you see? ;-)

You might need a "w" second argument for permanent_file =
file(permanent_name), by the way ... you are going to write the file, after
all.

I recently wrote a utility to scan several filestore directories and copy
the content into a date-time hierarchical directory structure, with the new
files named "hhmmss." plus the original name. Here are the extracts that
show another attempt (which I didn't try on Linux yet). You'll notice that
like you I'm too idle to spend an hour looking for shutil.copyfileobj() when
I could do it in Python in a few lines ;-)

...
import mx.DateTime as dt
...
for lang, lv in curs.fetchall():
    ...
    new_recordings = glob(os.path.join(intakedir, lang, "*.wav"))
    for f in new_recordings:
        fnam = os.path.basename(f)    # just the name, no path stuff
        crtime = os.stat(f)[7]        # creation date & time
        ...                           # next line gets time tuple items
        yyyy, mm, dd, HH, MM, SS = time.localtime(crtime)[:6]
        fnmake(archivedir, yyyy, mm, dd)  # ensures directory exists
        inf = file(f, "rb")           # open read and close for data
        data = inf.read()
        inf.close()
        localname = os.path.join(str(yyyy), str(mm), str(dd),
"%02d%02d%02d.%s" % (HH, MM, SS, fnam))
        outf = file(os.path.join(archivedir, localname), "wb")
        outf.write(data)
        outf.close()
        curs.execute("INSERT INTO Recording (RcdDateTime, RcdLang, RcdPath)
VALUES (?, ?, ?)",
                     (dt.DateTimeFromTicks(crtime), lv, localname))
        connection.commit()
        os.unlink(f)

I'm rather pleased with it, especially as it also logs the files into a
relational database to allow web-based call processing to begin. Though I
magine it will be now be picked over for hygiene and sanity at endless
length. I confess I originally omitted the binary attribute in file open
modes, which is all too easily done. Unlike omitting "w" when you want to
write, it can silently modify the data rather than raising an exception.

Anyway, a shot in the dark. It's been too long since I posted some actual
code.

One nice point: having been a good drone and provided the client with a
reliable-though-not-inspired Dreamweaver-based VBScript web solution (rather
than fight with them to get to do it in Python) they have now trusted me to
install 2.2.2 on their database server, and I anticipate being able to write
Python web applications as well soon. Yea! Woo, hoo!

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list