home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / andere sprachen / python-1.3 / lib / tempfile.py < prev    next >
Encoding:
Python Source  |  1996-07-16  |  1.2 KB  |  62 lines

  1. # Temporary file name allocation
  2. #
  3. # XXX This tries to be not UNIX specific, but I don't know beans about
  4. # how to choose a temp directory or filename on MS-DOS or other
  5. # systems so it may have to be changed...
  6.  
  7.  
  8. import os
  9.  
  10.  
  11. # Parameters that the caller may set to override the defaults
  12.  
  13. tempdir = None
  14. template = None
  15.  
  16.  
  17. # Function to calculate the directory to use
  18.  
  19. def gettempdir():
  20.     global tempdir
  21.     if tempdir == None:
  22.         try:
  23.             tempdir = os.environ['TMPDIR']
  24.         except (KeyError, AttributeError):
  25.             if os.name == 'posix':
  26.                 tempdir = '/usr/tmp' # XXX Why not /tmp?
  27.             if os.name == 'amiga':
  28.                 tempdir = 'T:'
  29.             else:
  30.                 tempdir = os.getcwd() # XXX Is this OK?
  31.     return tempdir
  32.  
  33.  
  34. # Function to calculate a prefix of the filename to use
  35.  
  36. def gettempprefix():
  37.     global template
  38.     if template == None:
  39.         if os.name == 'posix':
  40.             template = '@' + `os.getpid()` + '.'
  41.         else:
  42.             template = 'tmp' # XXX might choose a better one
  43.     return template
  44.  
  45.  
  46. # Counter for generating unique names
  47.  
  48. counter = 0
  49.  
  50.  
  51. # User-callable function to return a unique temporary file name
  52.  
  53. def mktemp():
  54.     global counter
  55.     dir = gettempdir()
  56.     pre = gettempprefix()
  57.     while 1:
  58.         counter = counter + 1
  59.         file = os.path.join(dir, pre + `counter`)
  60.         if not os.path.exists(file):
  61.             return file
  62.