[Python-Dev] Improvements for Pathlib
Wolfgang Langner
tds333+pydev at gmail.com
Mon Nov 10 09:38:12 CET 2014
Hi,
some background about my pathlib usage.
I use the backported version of pathlib and tried to replace the usage of
path.py (also widely used library).
Some features part of path.py missing in pathlib:
- expanduser, expandvars, I have to use the os functions to do this.
It is possible but better if this is part of pathlib.
- shutil stuff, as noted below
- dirs, and files from path.py. I often had to traverse a directory.
I know this is possible with a generator. But It is so often needed that I
missed this. Simple dirs(pattern) and files(pattern) is enough. With support
for "**" as recursion.
Pathlib is a great library and it was good to add it to the standard lib.
Now it is time to improve it further.
On 08.11.2014 16:46, Ionel Cristian Mărieș wrote:
> Hello,
>
> In the current incarnation Pathlib is missing some key features I need in my
> usecases. I want to contribute them but i'd like a bit of feedback on the new
> api before jumping to implementation.
>
> The four things I need are:
>
> #1. A context manager for temporary files and dirs (that cleans everything up
> on exit). Eg:
>
> with pathlib.TmpDir(suffix='', prefix='') as tmp:
> assert isinstance(tmp, pathlib.Path)
>
> with pathlib.TmpFile(suffix='', prefix='') as tmp:
> assert isinstance(tmp, pathlib.Path)
Good idea, but we should better add path support to tempfile module.
> #2. A context manager for changing working directory (that switches to the old
> cwd on exit). Eg:
>
> with path.cd <http://path.cd>():
> assert os.getcwd() == path
Also useful. Even if the state is global. But has to be noted in documentation.
> #
> 3
> . Shutil-like features:
>
> - copy_file
> - copy_dir (or copy_tree?)
> - copy (does copy_dir or copy_file depending on what the source is. How to
> handle symlinks?)
> - rm_dir (or rm_tree? Also, this would be required for the TmpDir path).
I also often needed this.
Thanks for bringing this up.
Regards,
Wolfgang
More information about the Python-Dev
mailing list