Hi Bruno,

On 4/13/24 3:17 AM, Bruno Haible wrote:
> +def rmtree(dest: str) -> None:
> +    '''Removes the file or directory tree at dest, if it exists.'''
> +    # These two implementations are nearly equivalent.
> +    # Speed: 'rm -rf' can be a little faster.
> +    # Exceptions: shutil.rmtree raises Python exceptions, e.g. 
> PermissionError.
> +    if True:
> +        sp.run(['rm', '-rf', dest], shell=False)
> +    else:
> +        try:
> +            shutil.rmtree(dest)
> +        except FileNotFoundError:
> +            pass

You should be able to use 'shutil.rmtree(dest, ignore_errors=True)'
here [1]. Unless you have a reason for not doing so that I missed.

>>> shutil.rmtree('bad-directory-name')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.12/shutil.py", line 775, in rmtree
    onexc(os.lstat, path, err)
  File "/usr/lib64/python3.12/shutil.py", line 773, in rmtree
    orig_st = os.lstat(path, dir_fd=dir_fd)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'bad-directory-name'
>>> shutil.rmtree('bad-directory-name', ignore_errors=True)
>>> shutil.rmtree('bad-directory-name', ignore_errors=True)

[1] https://docs.python.org/3/library/shutil.html#shutil.rmtree

Collin

Reply via email to