Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why os.rename sometimes returns error access is denied python

I used os.rename() method to rename the directory in my python script. This script called automatically by the scheduler every day. Sometimes the os.rename() function returns the error,

 [Error 5] Access is denied

But all other times its working fine. Code,

try:
  if(os.path.exists(Downloaded_Path)):
    os.rename(Downloaded_Path, Downloaded_Path + "_ByClientTool")
except Exception,e:
  print "Error !!", str(e)
  return 1
like image 311
user1553605 Avatar asked Jun 27 '26 05:06

user1553605


1 Answers

I had a similar problem on Windows 10: sometimes my python script could not rename a directory even though I could manually rename it without a problem.

I used Sysinternal's handle.exe tool to find that explorer.exe had a handle to a sub-directory of the directory I was trying to rename. It turns out explorer was adding this sub-directory to its "Quick Access" section which prevented my script from renaming the folder.

I wound up disabling the "Show frequently used folders in Quick access" option from Explorer -> View -> Options -> General -> Privacy.

like image 192
som-dev Avatar answered Jun 29 '26 19:06

som-dev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!