Permission problems when creating a dir with os.makedirs in Python

后端 未结 4 910
误落风尘
误落风尘 2020-12-03 04:41


I\'m simply trying to handle an uploaded file and write it in a working dir which name is the system timestamp. The problem is that I want to create that directory wi

4条回答
  •  [愿得一人]
    2020-12-03 05:06

    For Unix systems (when the mode is not ignored) the provided mode is first masked with umask of current user. You could also fix the umask of the user that runs this code. Then you will not have to call os.chmod() method. Please note, that if you don't fix umask and create more than one directory with os.makedirs method, you will have to identify created folders and apply os.chmod on them.

    For me I created the following function:

    def supermakedirs(path, mode):
        if not path or os.path.exists(path):
            return []
        (head, tail) = os.path.split(path)
        res = supermakedirs(head, mode)
        os.mkdir(path)
        os.chmod(path, mode)
        res += [path]
        return res
    

提交回复
热议问题