[Python-ideas] Pathlib additions & changes

David Townshend aquavitae69 at gmail.com
Mon Jun 22 10:30:26 CEST 2015


Hi

Recently I've been trying out pathlib in some real code, and while it is a
vast improvement on messing around with os, os.path and shutil there are a
couple of suggestions I'd like to make.

** TL;DR: Add Path.copy, add Path.remove (replaces Path.rmdir and
Path.unlink) and add flags to several methods.

Details
======

1.  Add a copy method, i.e. source_path.copy(target_path), which by default
should behave like shutil.copy2.

2.  Why bother with the distinction between Path.unlink and Path.rmdir?
Obviously they apply to different types of paths, but to a user, either way
you just want to remove whatever is at the path, so perhaps just have a
single Path.remove instead (but see point 3 below).

3.  There are several other minor irritations where a common pattern
requires several lines or the use of a lower-level library such as shutil.
For example:

  *  Mkdir where path exists, but we don't care (common pattern on scripts)
    if not path.exists():
        path.mkdir(parent=True)

  *  Recursively remove a directory (no sane way using pathlib alone)
    shutil.rmtree(str(path))

  *  Move a file, creating parents if necessary
    py> if not target.parent.exists():
            target.parent.mkdir(parents=true)
        source.rename(target)

There are others, but these are a couple that spring to mind.  There are
three options.  Either we add a bunch of specific functions for each of
these (e.g. Path.rmtree, Path.rename_with_mkdir, etc), or we add a whole
lot of boolean arguments (e.g. Path.rename(make_parents=True), or we use
flags, e.g. Path.rename(flags=MAKE_PARENTS)

Using flags is, IMHO the neatest solution, and could replace some boolean
arguments already included.  What follows is a suggestion of where flags
might be useful, including the new methods suggested above.  I haven't put
a huge amount of thought into these, wanting to just get the general idea
on the table, so I'm sure that upon closer inspection some won't make much
sense or could be better named.

    chmod: RECURSIVE | DONT_FOLLOW_SYMLINKS
    copy: WITH_STATS | MAKE_PARENTS | OVERWRITE_EXISTING | IGNORE_EXISTING
    iterdir: RECURSIVE (maybe not worth it because of globbing)
    lchmod: RECURSIVE (Could be dropped in favour of
chmod(flags=DONT_FOLLOW_SYMLINKS))
    lstat: (Could be dropped in favour of stat(flags=DONT_FOLLOW_SYMLINKS))
    mkdir: MAKE_PARENTS | OVERWRITE_EXISTING | IGNORE_EXISTING
    remove: RECURSIVE
    rename: MAKE_PARENTS | OVERWRITE_EXISTING | IGNORE_EXISTING
    replace: (Could be dropped in favour of rename(flags=OVERWRITE_EXISTING)
)
    rmdir: (Could be dropped in favour of remove)
    stat: DONT_FOLLOW_SYMLINKS
    touch: MAKE_PARENTS | IGNORE_EXISTING
    unlink: (Could be dropped in favour of remove)

Regards
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150622/45008648/attachment-0001.html>


More information about the Python-ideas mailing list