How can I get the default file permissions in Python?

岁酱吖の 提交于 2019-12-05 05:49:12

For the record, I had a similar issue, here is the code I have used:

import os
from tempfile import NamedTemporaryFile

def UmaskNamedTemporaryFile(*args, **kargs):
    fdesc = NamedTemporaryFile(*args, **kargs)
    umask = os.umask(0)
    os.umask(umask)
    os.chmod(fdesc.name, 0o666 & ~umask)
    return fdesc

There is a function umask in the os module. You cannot get the current umask per se, you have to set it and the function returns the previous setting.

The umask is inherited from the parent process. It describes, which bits are not to be set when creating a file or directory.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!