[Python-Dev] Improvements for Pathlib
Xavier Morel
python-dev at masklinn.net
Sat Nov 8 19:46:03 CET 2014
On 2014-11-08, at 16:46 , Ionel Cristian Mărieș <contact at ionelmc.ro> 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)
>
Why would pathlib need to provide this when tempfile already does?
with tempfile.NamedTemporaryFile(prefix='') as f:
tmp = pathlib.Path(f.name)
with tempfile.TemporaryDirectoryDirectory(prefix='') as d:
tmp = pathlib.Path(d.name)
On 2014-11-08, at 19:14 , Ryan Gonzalez <rymg19 at gmail.com> wrote:
> +1 for the context manager ideas. Anything that is like a resource should be available as a context manager.
The current working directory is not "a resource" though it's a big
piece of global state. I've been bitten by
context-manager-unexpectedly-being-global-state in the past (with
warnings.catch_warnings, I had not noticed the note about
thread-safety). All in all, not a fan.
More information about the Python-Dev
mailing list