File: files.py

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (27 lines) | stat: -rw-r--r-- 807 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import shutil
import time

def safe_rm_tree(path):
    if not os.path.isdir(path):
        return

    def onerror(func, path, exc_info):
        import stat

        # Is the error an access error?
        if not os.access(path, os.W_OK):
            print(f"Failed to remove `{path}`. Trying to change permissions...")
            os.chmod(path, stat.S_IWUSR)
            func(path)
        else:
            raise OSError("Cannot change permissions for {}! Exception info: {}".format(path, exc_info))

    for i in range(20):
        try:
            shutil.rmtree(path, onerror=onerror)
            return
        except Exception as e:
            delay = 0.5 * float(i + 1)
            print(f"Failed to remove `{path}`: `{e}`. Retrying in {delay} seconds...")
            time.sleep(delay)