1

I have a script which is suppose to recurively remove corrupt code I am working with.

import shutil
import subprocess

path = subprocess.check_output("echo %APPDATA%", shell=True)
path = "%s\\" % path[:-2]
shutil.rmtree("%sPub\\Cache\\" % path)

and it fails at the rmtree call. It looks like it fails in the rmtree call at:

os.remove(fullname)

and then it will throw the error saying:

WindowsError: [Error 5] Access is denied: '.............'

I have tried both running this in a regular shell and an admin shell and im not sure what is going on. Maybe the subprocess launched by os.remove is not admin? I figured those rights would be passed into the child processes forked by my python script.

Thoughts?

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • What is the actual directory that your code is attempting to remove? – Quietust Apr 05 '17 at 20:17
  • im trying to delete everything under the * Pub\Cache\ * directory. It is a bunch of stuff related to the outdated datasets from Dart's (The language) package management system – Fallenreaper Apr 05 '17 at 20:19
  • When you get the "Access denied" error, did any of the files get deleted? What happens if you try to delete the directory from Explorer? – Quietust Apr 05 '17 at 20:30
  • As a side note, there are cleaner ways to fetch environment variables (`os.environ['APPDATA']`) and build paths from individual components (`os.path.join(basepath,dir1,dir2,...)`). – Quietust Apr 05 '17 at 20:31
  • When deleting the items from explorer, they all delete. Was about 250mb worth of data clean up. Also, i really shouldve thought more on it. Should i have just did: `path = "%sPub\\Cache\\" % os.environ["APPDATA"]` ? – Fallenreaper Apr 05 '17 at 20:34
  • Your Python code isn't trying to just delete the contents of that directory - it's trying to delete the **directory itself**, an additional step that could fail. Your "Access is denied" message should have indicated which file it was unable to delete - was that file in use by another application at the time? – Quietust Apr 05 '17 at 20:40
  • yeah my goal was to delete the folder and all of its contents. The error was: WindowsError: [Error 5] Access is denied: 'C:\\Users\\wfrancis\\AppData\\Roaming\\Pub\\Cache\\git\\cache\\seg_ui-7212c90ba70f88fb83440f8049e5d6ee9e2276ef\\objects\\pack\\pack-053cf44e4ba619c904614642a957e2d44688ba54.idx' – Fallenreaper Apr 05 '17 at 20:41
  • It seems like there was a subfolder called git (as you can see in the above comment) that it didnt like. When i repopulated the area the script worked. Something in that git subdirectory though is not really liked by the `os.remove`function – Fallenreaper Apr 05 '17 at 20:46
  • Related: [How to avoid “WindowsError: \[Error 5\] Access is denied”](https://stackoverflow.com/q/37830326/3357935) – Stevoisiak Jun 13 '18 at 16:02

0 Answers0