Python Forum
os.remove() Access is denied
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.remove() Access is denied
#1
I'm receiving the below error when attempting to use os.remove() if a directory is found to exist. I've seen suggestions of running the script in an admin command prompt, that did not help. Relevant code snippet is also below.
Any help with understanding why this is happening is much appreciated!

Error:
Traceback (most recent call last): File "C:\Users\REMOVED\Python Apps\My Progs\test5.py", line 38, in <module> os.remove(final_dest) PermissionError: [WinError 5] Access is denied: 'C:\\Users\\REMOVED\\Python Apps\\My Progs\\test_folder\\test_folder'
final_dest = os.path.join(dest, basename)
if os.path.exists(final_dest):
    os.remove(final_dest)
    os.mkdir(final_dest)
else:
    os.mkdir(final_dest)
Reply
#2
If you have a file, you can remove just that file with os.remove() or os.unlink(). Similar to /bin/rm, this function fails on directories.

If you have a (empty) directory, you can remove it with os.rmdir(). Similar to /bin/rmdir.

If you have a path and want it and everything underneath it removed if possible, you can do so with shutil.rmtree(). Similar to /bin/rm -r.

Also, older documentation stated that os.remove() would throw an OSError if it were handed a directory. Newer documentation states flatly that it now throws a IsADirectoryError. But it looks like that may be OS dependent. I do see the newer error on Linux, but I don't on MacOS (which still throws OSError, even on 3.8). I'm guessing that Windows does the same, but I can't test that immediately.

Linux/Python 3.7.4
>>> os.remove("dir")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IsADirectoryError: [Errno 21] Is a directory: 'dir'
MacOS/Python 3.8.2
>>> os.remove("dir")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 1] Operation not permitted: 'dir'
Reply
#3
Thank you for the explanation. shutil.rmtree() works great.

So the os module isn't capable to deleting a directory with contents?
Reply
#4
Most (all?) things in "os" are access to the operating system functions rather than implemented in python. So yes, you're correct. unlink() is the OS call to remove a file and it often fails when handed a directory. Python isn't really doing anything here other than handing the structure over to the OS and handing back any success/failure information.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sharepoint: Access has been blocked by Conditional Access policies CAD79 0 1,727 Jul-12-2024, 09:36 AM
Last Post: CAD79
  Circumvent the "access denied" page? Pedroski55 7 1,803 Jun-15-2024, 06:25 AM
Last Post: Pedroski55
  The INSERT permission was denied on the object Steven5055 3 2,700 Jun-12-2024, 08:13 AM
Last Post: GregoryConley
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 2,604 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  Server Folder Error : WinError5 Access Denied fioranosnake 1 1,639 Jun-21-2022, 11:11 PM
Last Post: Larz60+
  Error no 13: Permission denied in python shantanu97 1 7,857 Mar-31-2021, 02:15 PM
Last Post: snippsat
  Access denied not able to do anything 8( What do i do? MurphysLaw 3 4,795 Oct-20-2020, 08:16 AM
Last Post: snippsat
  Fixing "PermissionError: [Errno 13] Permission denied" puredata 17 84,484 Mar-09-2020, 03:20 PM
Last Post: syssy
  Has anyone experience a winError[5] Access Denied in Windows 10? fstkmaro 2 15,733 Nov-11-2019, 02:38 PM
Last Post: fstkmaro
  PermissionError: [Errno 13] Permission denied: error leviathan54 2 48,630 Apr-20-2019, 12:51 AM
Last Post: leviathan54

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020