classification
Title: pathlib.Path wants an rmtree method
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Aaron Hall, Dreami, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-05-14 14:30 by Aaron Hall, last changed 2018-09-14 07:59 by Dreami.

Messages (4)
msg316511 - (view) Author: Aaron Hall (Aaron Hall) * Date: 2018-05-14 14:30
pathlib.Path wants the rmtree method from shutil

I think we need this method for a couple of reasons.

1. in shell, rm has the -r flag - In Python, we use shutil.rmtree as a best practice for this.

2. I prefer to teach my students about pathlib.Path as opposed to other ways of dealing with files. It's a great abstraction. But it's somewhat leaky, especially when it comes to recursively deleting a directory with its contents, as I now need to import rmtree from shutil.

Perhaps we need this as a method in the abstract base class that recursively uses the methods provided by the concrete implementations. I can look at the rmtree method for a reference implementation. 

Perhaps we should just give Path.rmdir a default recursive argument? Default would be False, of course, to retain current behavior.
msg316517 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-14 15:10
What is wrong with just using shutil.rmtree()?

You can't deal with files with only using pathlib. You can't read ZIP archives, open temporary files, import modules, download files from Internet and open them in the webbrowser, run subprocesses, send files by e-mail, determine the MIME type of files, read WAV files with only using pathlib.
msg316676 - (view) Author: Aaron Hall (Aaron Hall) * Date: 2018-05-15 16:08
> What is wrong with just using shutil.rmtree()?

0. It's awkward to import just for demonstrations.
1. It's harder for new pythonists to discover.
2. A method provides discoverability in an object's namespace.
3. rmtree is a method of paths (typical usage is rmtree(path)).
4. rmtree is clearly functionality that is missing from the Path object (which has effectively rm, rm -d, but not rm -r).

> You can't deal with files with only using pathlib. You can't read ZIP archives, open temporary files, import modules, download files from Internet and open them in the webbrowser, run subprocesses, send files by e-mail, determine the MIME type of files, read WAV files with only using pathlib.

I wasn't suggesting those things. After some thought, I would probably not support those things to be in pathlib either. Maybe they are "file" methods, but to me, they are not semantically "path" methods. That functionality is in much more specialized domain-oriented modules, and easy to discover. 

We need a recursive rmdir so that users aren't tempted to roll their own - and wind up deleting symlinked things.

I *would* support some of those other shutil functions to become Path methods, perhaps move, copy2, and copytree, as they *are* path methods (you just need to supply another destination path), but I'm not finding it to be a pain point yet.
msg325332 - (view) Author: Lukas Röllin (Dreami) Date: 2018-09-14 07:59
I definitely would like this too. I have a code base that's only grazing on path usage, and I could completely convert it to pathlib, but one shutil.rmtree still remains...
History
Date User Action Args
2018-09-14 07:59:40Dreamisetnosy: + Dreami
messages: + msg325332
2018-05-15 16:08:09Aaron Hallsetmessages: + msg316676
2018-05-14 15:10:11serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg316517
2018-05-14 14:30:35Aaron Hallcreate