简体   繁体   中英

Python: create a directory with 777 permissions

I'm making a program that creates a folder using os.makedirs("foo") . But when I open Windows 10 files explorer and right click on the new directory that has been created, I noticed that it was not possible to delete the folder without administrator privileges. So how do you create a folder with the permission to recursively delete this folder ?

I create the directory using: os.makedirs("data/base/{}".format(args[0].text), mode=0o777)

I delete it using:

def delete_class(self, *args):
        for root, dirs, files in os.walk("data/base/{}".format(self.clicked_class_to_delete.id), topdown=False):
            for name in files:
                filename = os.path.join(root, name)
                os.chmod(filename, stat.S_IWUSR)
                os.remove(filename)
            for name in dirs:
                os.rmdir(os.path.join(root, name))
        shutil.rmtree("data/base/{}".format(self.clicked_class_to_delete.id)

)

也许你需要做的os.umask(0)之前os.makedir ,去除掩模为当前用户。

只需这样做:

os.makedirs(name,0777)

From the documentation: os.makedirs(name, mode=0o777, exist_ok=False)

You can find the documentation here .

Time stamped based directory. With full permission.

now = datetime.datetime.now().strftime("%y%m%d%H%M")
dirName = "/var/name-{}".format(now)
os.umask(0)
os.makedirs(dirName,mode=0o777)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM