sysgettempdir php,Python`临时文件.gettempdir()`不尊重TMPDIR

目录是否存在?用户可以写吗?否则,它将无法工作,Python将退回到其他位置。您可以看到代码here,此处复制以供参考: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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值