我可以在python中为tempfile.NamedTemporaryFile设置umask吗? [英] Can I set the umask for tempfile.NamedTemporaryFile in python?

查看:216
本文介绍了我可以在python中为tempfile.NamedTemporaryFile设置umask吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆