Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pathlib.Path wants an rmtree method #77679

Closed
aaronchall mannequin opened this issue May 14, 2018 · 7 comments
Closed

pathlib.Path wants an rmtree method #77679

aaronchall mannequin opened this issue May 14, 2018 · 7 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@aaronchall
Copy link
Mannequin

aaronchall mannequin commented May 14, 2018

BPO 33498
Nosy @pitrou, @giampaolo, @tarekziade, @serhiy-storchaka, @aaronchall, @lroellin
PRs
  • bpo-33498: Add rmtree method to pathlib.Path #11504
  • bpo-33498: Add rmtree method to pathlib.Path #11504
  • bpo-33498: Add rmtree method to pathlib.Path #11504
  • bpo-33498: Add rmtree method to pathlib.Path #11504
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-01-10.15:45:10.052>
    created_at = <Date 2018-05-14.14:30:35.303>
    labels = ['3.8', 'type-feature', 'library']
    title = 'pathlib.Path wants an rmtree method'
    updated_at = <Date 2020-06-14.07:48:31.468>
    user = 'https://github.com/aaronchall'

    bugs.python.org fields:

    activity = <Date 2020-06-14.07:48:31.468>
    actor = 'Charles Machalow'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-01-10.15:45:10.052>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2018-05-14.14:30:35.303>
    creator = 'Aaron Hall'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33498
    keywords = ['patch', 'patch', 'patch', 'patch']
    message_count = 7.0
    messages = ['316511', '316517', '316676', '325332', '333387', '333389', '371497']
    nosy_count = 7.0
    nosy_names = ['pitrou', 'giampaolo.rodola', 'tarek', 'serhiy.storchaka', 'Charles Machalow', 'Aaron Hall', 'Dreami']
    pr_nums = ['11504', '11504', '11504', '11504']
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue33498'
    versions = ['Python 3.8']

    @aaronchall
    Copy link
    Mannequin Author

    aaronchall mannequin commented May 14, 2018

    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.

    @aaronchall aaronchall mannequin added 3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 14, 2018
    @serhiy-storchaka
    Copy link
    Member

    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.

    @aaronchall
    Copy link
    Mannequin Author

    aaronchall mannequin commented May 15, 2018

    What is wrong with just using shutil.rmtree()?

    1. It's awkward to import just for demonstrations.
    2. It's harder for new pythonists to discover.
    3. A method provides discoverability in an object's namespace.
    4. rmtree is a method of paths (typical usage is rmtree(path)).
    5. 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.

    @lroellin
    Copy link
    Mannequin

    lroellin mannequin commented Sep 14, 2018

    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...

    @serhiy-storchaka
    Copy link
    Member

    I am against this. shutil provides higher level API than pathlib, and pathlib should not depend on shutil. pathlib.Path represents a path, and its methods usually does involve filesystem operations, or involve just a single filesystem operation. shutil.rmtree() is a complex operation that needs to perform several simple operations, and it should be together with other complex operations in the shutil module.

    @pitrou
    Copy link
    Member

    pitrou commented Jan 10, 2019

    Indeed there is no reason to put all useful path informations under the Path class. Writing method calls instead of function calls is not an ideal.

    @pitrou pitrou closed this as completed Jan 10, 2019
    @CharlesMachalow
    Copy link
    Mannequin

    CharlesMachalow mannequin commented Jun 14, 2020

    I'm disappointed to see this closed. For new (and old) users, it makes complete sense to have an rmtree method on the Path object itself.

    If I'm using pathlib, I try not to delegate to os.<things> for file operations, since it also tends to seem more OO and clean to stick with the object methods. Same thought process for shutil for rmtree.

    We already have things like owner() and group() that explicitly go import grp, pwd to do their job on Path objects. Why is shutil special with regards to using another piece of the standard library from pathlib? To me this request falls in the same boat as those functions; they were likely added for convenience and since it made sense.

    I believe the very same holds true for the request to add rmtree().

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants