home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / tempfile.py < prev    next >
Text File  |  1993-12-29  |  1KB  |  60 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.             else:
  28.                 tempdir = os.getcwd() # XXX Is this OK?
  29.     return tempdir
  30.  
  31.  
  32. # Function to calculate a prefix of the filename to use
  33.  
  34. def gettempprefix():
  35.     global template
  36.     if template == None:
  37.         if os.name == 'posix':
  38.             template = '@' + `os.getpid()` + '.'
  39.         else:
  40.             template = 'tmp' # XXX might choose a better one
  41.     return template
  42.  
  43.  
  44. # Counter for generating unique names
  45.  
  46. counter = 0
  47.  
  48.  
  49. # User-callable function to return a unique temporary file name
  50.  
  51. def mktemp():
  52.     global counter
  53.     dir = gettempdir()
  54.     pre = gettempprefix()
  55.     while 1:
  56.         counter = counter + 1
  57.         file = os.path.join(dir, pre + `counter`)
  58.         if not os.path.exists(file):
  59.             return file
  60.