shreddd shreddd - 1 year ago 87
Python Question

Can I set the umask for tempfile.NamedTemporaryFile in python?

In Python (tried this in 2.7 and below) it looks like a file created using

doesn't seem to obey the umask directive:

import os, tempfile
f1 = open ("goodfile", "w")
f2 = tempfile.NamedTemporaryFile(dir='.')

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

Any idea why
won't pick up the umask? Is there any way to do this during file creation?

I can always workaround this with os.chmod(), but I was hoping for something that did the right thing during file creation.

Answer Source

This is a security feature. The NamedTemporaryFile is always created with mode 0600, hardcoded at, 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download