2

I'm processing a file and then want to remove it. My code ends with:

...
tar.extractall(dir=...)
tar.close()
os.remove(filename)

This sometimes causes an error:

[Errno 16] Resource busy: 'skyimage/sgptsiskyimageC1.a1.20160415.000000.jpg.tar'

I was able to work around this by telling Python to keep trying until it works:

while True:
    try:
        os.remove(filename)
        break
    except OSError as err:
        continue

Is there a more Pythonic way to do this?

Peter Drake
  • 443
  • 4
  • 17
  • This question may actually be a duplicate. There is a ton of discussion on this topic and related points at: https://stackoverflow.com/questions/6996603/how-to-delete-a-file-or-folder Have a look at that stuff and learn more than you ever thought possible on removing files and folders and directories in Python. – Dr t May 26 '17 at 18:44
  • 1
    is that some special filesystem? like dropbox or other? – Jean-François Fabre May 26 '17 at 18:45
  • You didn't show the whole code. Maybe you opened the file before but didn't close it? – Liran Funaro May 26 '17 at 19:05
  • Your code is reasonable but I would add a short sleep and consider a timeout and error path if the problem doesn't resolve in a reasonable amount of time. – tdelaney May 26 '17 at 19:09

0 Answers0