[Python-ideas] New PEP proposal -- Pathlib Module Should Contain All File Operations

George Fischhof george at fischhof.hu
Thu Mar 15 09:38:04 EDT 2018


2018-03-13 13:17 GMT+01:00 Steven D'Aprano <steve at pearwood.info>:

> On Mon, Mar 12, 2018 at 09:57:32PM +0100, George Fischhof wrote:
>
> > Right now we have several modules that contain functions related
> > to file-system operations mainly the os, pathlib and shutil.
> > For beginners it is quite hard to remember where can he / she find
> > a function (copy resides in shutil, but the remove function can be
> > found in the os module.  (And sometimes developers with moderate
> > experience have to check the documentation as well.)
>
> This is certainly a problem. Not a big problem, but it is an annoyance.
>
>
> > With functions in pathlib the developers should not have to think
> > on which method (function) can be found in which module.
> >
> > Makes the life easier.
>
> I don't know that this will be true. It makes one problem better: you no
> longer have to remember which module the function is in. But it makes
> other things worse:
>
> - code and/or API duplication: for backwards compatibility, every
>   existing function must be in two places, the original and in
>   pathlib;
>
> - if new file functions are added, they will go only in pathlib,
>   which makes pathlib effectively mandatory;
>
> - the pathlib API becomes even more complicated: not only have you
>   got all the methods of pathlib objects, but you have all the shutil
>   and os functions as well.
>
>
> I think this is a good place for an experiment. You could write a
> function which monkey-patches pathlib:
>
> from pathlib import Path
> import os
> import shutil
>
> def monkeypatch():
>     Path.remove = os.remove
>     # etc.
>
> Then we can see how many functions are involved, how large this makes
> the Path object, and try it out and see whether it is better.
>
>
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



Duplication: it is true, but it is true for several other modules as well.
I checked the pathlib module: right now more than 50% of the functions are
duplicate
- mainly from os - so it seems that pathlib already started to develop this
way ;-)


" if new file functions are added, they will go only in pathlib,
  which makes pathlib effectively mandatory;"
Yes but I think this part of the evolution: slowly everyone will shift to
pathlib,
and being mandatory is true for the current status as well: if you need a
function, you need the module.
Right now if you wan to execute some file operations, you need os plus
shutil, because the half of the
functions are in one of them, the other half is in the other module


I collected the functions that sould be put into pathlib:

- os.remove

- os.removedirs (shutil.rmtree has the same functionalaty)

- os.truncate

- shutil.copyfileobj

- shutil.copyfile

- shutil.copymode

- shutil.copystat

- shutil.copy

- shutil.copy2

- shutil.copytree with shutil.ignore_patterns

- shutil.move

- shutil.disk_usage

- shutil.chown

- os.link => path.hardlink_to

- os.mkfifo

- os.readlink


Sum: 16 functuins
And  all functions from os module accept path-like objects, and none of the
shutil functions.
Pathlib already contains 17 functions from os an shutil modules.


George
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180315/1e5bac35/attachment-0001.html>


More information about the Python-ideas mailing list