bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gnulib-tool.py: Refactor directory tree removals


From: Collin Funk
Subject: Re: gnulib-tool.py: Refactor directory tree removals
Date: Sat, 13 Apr 2024 03:39:25 -0700
User-agent: Mozilla Thunderbird

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

[Prev in Thread] Current Thread [Next in Thread]