问题描述
在Python中(在2.7及更低版本中进行了尝试),看起来使用tempfile.NamedTemporaryFile
创建的文件似乎不遵循umask指令:
In Python (tried this in 2.7 and below) it looks like a file created using tempfile.NamedTemporaryFile
doesn't seem to obey the umask directive:
import os, tempfile
os.umask(022)
f1 = open ("goodfile", "w")
f2 = tempfile.NamedTemporaryFile(dir='.')
f2.name
Out[33]: '/Users/foo/tmp4zK9Fe'
ls -l
-rw------- 1 foo foo 0 May 10 13:29 /Users/foo/tmp4zK9Fe
-rw-r--r-- 1 foo foo 0 May 10 13:28 /Users/foo/goodfile
有人知道为什么NamedTemporaryFile
无法接收umask吗?在文件创建过程中有什么方法可以做到这一点?
Any idea why NamedTemporaryFile
won't pick up the umask? Is there any way to do this during file creation?
我总是可以使用os.chmod()解决此问题,但是我希望能在文件创建过程中做对的事情.
I can always workaround this with os.chmod(), but I was hoping for something that did the right thing during file creation.
推荐答案
这是一项安全功能. NamedTemporaryFile
始终以模式0600
创建,硬编码为 tempfile.py
235 ,因为它对您的进程是私有的,直到您使用chmod
打开它为止.没有构造函数参数可以更改此行为.
This is a security feature. The NamedTemporaryFile
is always created with mode 0600
, hardcoded at tempfile.py
, line 235, because it is private to your process until you open it up with chmod
. There is no constructor argument to change this behavior.
这篇关于我可以在python中为tempfile.NamedTemporaryFile设置umask吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!