Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

)

like image 627
Mike Delta Avatar asked Jun 04 '26 19:06

Mike Delta


1 Answers

Maybe you need to do os.umask(0) before os.makedir, to remove the mask for the current user.

like image 121
Garvit Jain Avatar answered Jun 07 '26 19:06

Garvit Jain