Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using python os.rmdir, get PermissionError: [WinError 5] Access is denied

I'm building a file-transfer script, and the source cleanup function makes use of os.rmdir('C:\\Users\\Grav\\Desktop\\TestDir0\\Om'). This is the error I get:

PermissionError: [WinError 5] Access is denied: 'C:\\Users\\Grav\\Desktop\\TestDir0\\Om'

I checked the permissions on the folder Om through the Windows 7 and they are set to allow deletion for my user account. I've also tried setting my interpreter to run as admin. The problem persists and I'm stymied. Much obliged to anyone with insight!

like image 763
Grav Avatar asked Apr 01 '16 15:04

Grav


People also ask

How do I fix access denied in Python?

The PermissionError: [errno 13] permission denied error occurs when you try to access a file from Python without having the necessary permissions. To fix this error, use the chmod or chown command to change the permissions of the file so that the right user and/or group can access the file.

How do you delete a directory in Python?

Deleting file/dir using the os.rmdir() method in Python is used to remove or delete an empty directory. OSError will be raised if the specified path is not an empty directory.

How do you delete a file in Python?

remove() method in Python can be used to remove files, and the os. rmdir() method can be used to delete an empty folder. The shutil. rmtree() method can be used to delete a folder along with all of its files.


2 Answers

uncheck read-only attribute box found in the properties of the file/folder. enter image description here

like image 163
Osama Adly Avatar answered Oct 25 '22 21:10

Osama Adly


I found a solution here: What user do python scripts run as in windows?

It seems as if the offending folder has a stubborn read-only attribute. Adding in a handler to change such read-only flags worked like a charm for me.

All of you who posted suggestions, you helped me track down the final answer, so thank you!

like image 24
Grav Avatar answered Oct 25 '22 21:10

Grav