Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python`tempfile.gettempdir()`不尊重TMPDIR_Python_Temporary Files - Fatal编程技术网

Python`tempfile.gettempdir()`不尊重TMPDIR

Python`tempfile.gettempdir()`不尊重TMPDIR,python,temporary-files,Python,Temporary Files,我正在使用一个共享linux集群,希望我的临时目录是/tmp/username,而不是默认的/tmp 根据gettempdir()应该使用$TMPDIR环境变量来确定临时目录 然而,情况似乎并非如此 $ export TMPDIR='/tmp/username' $ python -c "\ import os import tempfile print(os.environ['TMPDIR']) print(tempfile.gettempdir())" /tmp/username /tmp

我正在使用一个共享linux集群,希望我的临时目录是
/tmp/username
,而不是默认的
/tmp

根据gettempdir()应该使用
$TMPDIR
环境变量来确定临时目录

然而,情况似乎并非如此

$ export TMPDIR='/tmp/username' 
$ python -c "\
import os
import tempfile
print(os.environ['TMPDIR'])
print(tempfile.gettempdir())"
/tmp/username
/tmp

有什么建议吗?

目录是否存在?它可由用户写入吗?否则,它将无法工作,Python将退回到其他位置。您可以看到代码,此处复制以供参考:

def _get_default_tempdir():
    """Calculate the default directory to use for temporary files.
    This routine should be called exactly once.

    We determine whether or not a candidate temp dir is usable by
    trying to create and write to a file in that directory.  If this
    is successful, the test file is deleted.  To prevent denial of
    service, the name of the test file must be randomized."""

    namer = _RandomNameSequence()
    dirlist = _candidate_tempdir_list()
    flags = _text_openflags

    for dir in dirlist:
        if dir != _os.curdir:
            dir = _os.path.normcase(_os.path.abspath(dir))
        # Try only a few names per directory.
        for seq in xrange(100):
            name = namer.next()
            filename = _os.path.join(dir, name)
            try:
                fd = _os.open(filename, flags, 0o600)
                try:
                    try:
                        with _io.open(fd, 'wb', closefd=False) as fp:
                            fp.write(b'blat')
                    finally:
                        _os.close(fd)
                finally:
                    _os.unlink(filename)
                return dir
            except (OSError, IOError) as e:
                if e.args[0] == _errno.EEXIST:
                    continue
                if (_os.name == 'nt' and e.args[0] == _errno.EACCES and
                    _os.path.isdir(dir) and _os.access(dir, _os.W_OK)):
                    # On windows, when a directory with the chosen name already
                    # exists, EACCES error code is returned instead of EEXIST.
                    continue
                break # no point trying more names in this directory
    raise IOError, (_errno.ENOENT,
                    ("No usable temporary directory found in %s" % dirlist))

抢手货我在Python的bugtracker上发布了一个问题来改进文档: