0

在 Python 中创建临时目录时,我看到不一致的行为:

# System Python, Windows Console

C:\Python33>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\windows\\temp\\tmpte7fcc'


# Virtualenv Python, Windows Console

Scripts>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\windows\\temp\\tmprziefb'

# System Python, Cygwin Console

$ /cygdrive/c/Python33/python.exe
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\windows\\temp\\tmprk4fcu'

# Virtualenv Python, Cygwin Console

$ Scripts/python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\cygwin\\tmp\\tmppeozcl

C:\Windows\Temp前三种情况在(如预期的那样)创建临时目录。为什么第四种情况会在其他地方创建临时目录?


编辑:评论中要求的其他数据:

# System Python, Windows Console

C:\Python33>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\Python33']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP']


# Virtualenv Python, Windows Console

Scripts>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\_PROJECTS\\python-veracity']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP']

# System Python, Cygwin Console

$ /cygdrive/c/Python33/python.exe
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\Cygwin\\tmp', 'C:\\WINDOWS\\TEMP', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\_PROJECTS\\python-veracity']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\Cygwin\\tmp', 'C:\\WINDOWS\\TEMP']

# Virtualenv Python, Cygwin Console

$ Scripts/python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\Cygwin\\tmp', 'C:\\Cygwin\\tmp', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\_PROJECTS\\python-veracity']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\Cygwin\\tmp', 'C:\\Cygwin\\tmp']
4

1 回答 1

1

tempfile 模块查找环境变量 TMPDIR、TEMP 和 TMP。如果设置了这些变量之一,则其值将用作临时文件和目录的基本目录。我猜 Cygwin 将其中一个设置为 C:\cygwin\tmp。

于 2013-09-18T15:01:05.073 回答