7

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)

)

4 Answers 4

14

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

3
  • Thanks, this solves the problem for me! Could you please elaborate a bit more on what os.umask(0) actually does? I am looking in the documentation but I am a bit lost...
    – Tropilio
    Commented May 7, 2020 at 10:38
  • 1
    In simple terms, the user mask (umask) restricts the permissions that can be set by the program. Setting umask(0) removes this property, so whatever permissions are set by the program will be directly reflected. In my case, my celery worker was unable to write folders created by my main program unless I did this. Commented May 7, 2020 at 12:30
  • @GarvitJain your solution is good and work. i noticed that for some reason i can do from the bash shell mkdir --mode=0777 hello and this shell command works. so somehow using the shell mkdir command works but the python os.mkdir does not. Commented Oct 22, 2021 at 18:27
11

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

You can find the documentation here.

4
  • I edited my topic with more details, the problem persists
    – Mike Delta
    Commented Dec 5, 2017 at 21:27
  • @MikeDelta, just to be sure - you are able to create the folder and delete it with the code segment that you added, but you can't delete it "manually" without admin permissions. Am I right?
    – Mixhab
    Commented Dec 6, 2017 at 10:32
  • Sometimes I can't delete it from the file explorer, but often my interpreter gives an error like OS error: permission denied or error: file not empty or something like this
    – Mike Delta
    Commented Dec 6, 2017 at 13:20
  • @MikeDelta Try to localize the problem. Next time you get the error check if the folder is actually not empty. Need to understand which of your commands failing to be properly executed. You can run your code a few times only with the first for loop to see if it's executed properly each time. Then add the second for loop. And only then add shutil.rmtree
    – Mixhab
    Commented Dec 6, 2017 at 13:58
6

Just do this:

os.makedirs(name,0777)
3
  • I edited my topic with more details, the problem persists.
    – Mike Delta
    Commented Dec 5, 2017 at 21:23
  • This does not work. You can follow this anwser: stackoverflow.com/a/67723702/8718377
    – veben
    Commented May 27, 2021 at 14:05
  • In python3, this needs to be 0o777 otherwise you get "SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers" -- but there's no point in passing 777 because it's the default.
    – Carl Walsh
    Commented May 13 at 22:36
2

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.