os independent temp dir?

Mark Hadfield m.hadfield at niwa.cri.nz
Wed May 9 18:29:55 EDT 2001


Responding to a couple of different posts in this thread:

> On the Mac it appears you should store files in the "Temporary Items"
> folder of the boot volume (don't ask me how you find the boot volume).

Luckily, one of the contributors to the tempfile module did know that,
witness the following code snippet.

    elif os.name == 'mac':
        import macfs, MACFS
        try:
            refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk,
                                             MACFS.kTemporaryFolderType, 1)
            dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
            attempdirs.insert(0, dirname)
        except macfs.error:
            pass

Read the source, Luke!

When you do you will also see

    if os.name == 'nt':
        attempdirs.insert(0, 'C:\\TEMP')
        attempdirs.insert(0, '\\TEMP')

and later

    for envname in 'TMPDIR', 'TEMP', 'TMP':
        if os.environ.has_key(envname):
            attempdirs.insert(0, os.environ[envname])

(Question, what is os.name on Windows 95/98/ME? I think it's 'nt'.)

So tempfile makes a pretty thorough attempt to find an os-independent
tempdir, and if you have a system on which it doesn't work, you should
either:

  1. Consider the possibility that your system may be mis-configured

  2. Propose a patch to tempfile that covers your case, preferably without
further cluttering up the code any more.

It is my experience that the Windows installer normally sets up a TEMP
directory under the Windows directory, and a TEMP environment variable
pointing to it, so any system lacking one can be considered mis-configured.
But perhaps this is not true of 98 and/or ME.

---
Mark Hadfield
m.hadfield at niwa.cri.nz  http://katipo.niwa.cri.nz/~hadfield
National Institute for Water and Atmospheric Research


-- 
Posted from clam.niwa.cri.nz [202.36.29.1] 
via Mailgate.ORG Server - http://www.Mailgate.ORG



More information about the Python-list mailing list