Can I set the umask for tempfile.NamedTemporaryFile in python?
In case it might help someone, I wanted to do more or less the same thing, here is the code I have used:
import os
from tempfile import NamedTemporaryFile
def UmaskNamedTemporaryFile(*args, **kargs):
fdesc = NamedTemporaryFile(*args, **kargs)
# we need to set umask to get its current value. As noted
# by Florian Brucker (comment), this is a potential security
# issue, as it affects all the threads. Considering that it is
# less a problem to create a file with permissions 000 than 666,
# we use 666 as the umask temporary value.
umask = os.umask(0o666)
os.umask(umask)
os.chmod(fdesc.name, 0o666 & ~umask)
return fdesc
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.