cpython-withatomic / Lib / tempfile.py

The branch 'legacy-trunk' does not exist.
# Temporary file name allocation
# XXX This tries to be not UNIX specific, but I don't know beans about
# how to choose a temp directory or filename on MS-DOS or other
# systems so it may have to be changed...

import os

# Parameters that the caller may set to override the defaults

tempdir = None
template = None

# Function to calculate the directory to use

def gettempdir():
	global tempdir
	if tempdir == None:
			tempdir = os.environ['TMPDIR']
		except (KeyError, AttributeError):
			if os.name == 'posix':
				tempdir = '/usr/tmp' # XXX Why not /tmp?
				tempdir = os.getcwd() # XXX Is this OK?
	return tempdir

# Function to calculate a prefix of the filename to use

def gettempprefix():
	global template
	if template == None:
		if os.name == 'posix':
			template = '@' + `os.getpid()` + '.'
			template = 'tmp' # XXX might choose a better one
	return template

# Counter for generating unique names

counter = 0

# User-callable function to return a unique temporary file name

def mktemp():
	global counter
	dir = gettempdir()
	pre = gettempprefix()
	while 1:
		counter = counter + 1
		file = os.path.join(dir, pre + `counter`)
		if not os.path.exists(file):
			return file
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.