Forums

Can't delete a directory from a python script.

HI,in my flask app i tried using multiple commands to delete a not empty directory:

shutil.rmtree(path)

The first says dir not empty which is strange because this cmd supposed to delete not empty dirs

os.remove(os.path.join(path,filename))

os.rmdir(path)

The two above raises this error OSError: [Errno 16] Device or resource busy

Third i tried executing linux commands with the help of os module

os.system('rm -rf "{}"'.format(path))

this only deletes the files but the dir is still present and to clarify these commands works perfectly in my computer before uploading the changes to pythonanywhere. and thank you in advance for taking time to read my question and trying to help

The "Device or resource busy" could mean that you have an open file that you try to delete, or that something (other process) is trying to write into the directory that you're trying to delete. I think that other errors that you got may support this hypothesis, since (assuming you provided correct paths to your script), you got an error saying that directory is not empty, so something could have written to it while your script was trying to delete it. Does it seem plausible?

Thank you that now i understand what "Device or resource busy" means,but the problem only happens in pythonanywhere server but not in my local server, i use linux in my computer for development and every solution i posted above works as expected.what is left now is try to change how things work in my web app and get rid of trying to delete a directory to fix the issue.

That's part of the regular development cycle between a local environment and a server ("production") environment -- while debugging you need to diagnose wether it's a problem related to the environment or to the code architecture (there are cases which would not be caught on the local machine since it behaves differently).