(py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
[python/dscho.git] / Lib / tempfile.py
blobdb750d4a6e16c9b8f0ef8e72802f416664f6c9ac
1 # Temporary file name allocation
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...
8 import os
11 # Parameters that the caller may set to override the defaults
13 tempdir = None
14 template = None
17 # Function to calculate the directory to use
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
32 # Function to calculate a prefix of the filename to use
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
44 # Counter for generating unique names
46 counter = 0
49 # User-callable function to return a unique temporary file name
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