classification
Title: "chdir" Contex manager for pathlib
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Jáchym Barvínek, desbma, matrixise, pitrou, serhiy.storchaka
Priority: normal Keywords:

Created on 2015-11-14 17:45 by Jáchym Barvínek, last changed 2015-11-20 19:32 by matrixise. This issue is now closed.

Messages (7)
msg254664 - (view) Author: Jáchym Barvínek (Jáchym Barvínek) Date: 2015-11-14 17:45
I use this context manager in my code:

@contextmanager
def in_directory(path):
    pwd = str(Path().absolute())
    if not path.is_dir():
        path = path.parent
    os.chdir(str(path))
    yield path.absolute()
    os.chdir(pwd)

I thought it would be nice to have something like this in the pathlib as a method of Path library, I find it quite convenient, especially when dealing with subprocesses.
msg254665 - (view) Author: desbma (desbma) * Date: 2015-11-14 18:58
I'm not a Python core developer, but a few thoughts:
* this is not thread safe
* subprocess.Popen already has a cwd parameter for similar purpose
msg254667 - (view) Author: Jáchym Barvínek (Jáchym Barvínek) Date: 2015-11-14 19:11
I did not know about cwd for Popen. It seems like the better way to go.
Thank you.

2015-11-14 19:58 GMT+01:00 desbma <report@bugs.python.org>:

>
> desbma added the comment:
>
> I'm not a Python core developer, but a few thoughts:
> * this is not thread safe
> * subprocess.Popen already has a cwd parameter for similar purpose
>
> ----------
> nosy: +desbma
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue25625>
> _______________________________________
>
msg255003 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-11-20 17:47
What's the relation with subprocess.Popen ?
msg255004 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-11-20 18:14
I agree with desbma.
msg255005 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-11-20 18:17
> What's the relation with subprocess.Popen ?

In the context of subprocesses there is better way to run a program in specified working directory that temporary change current working directory in parent process.
msg255012 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-11-20 19:32
> In the context of subprocesses there is better way to run a program in specified working directory that temporary change current working directory in parent process.

Ok, haven't read the last line of the description. thanks for the
clarification.
-- 
Stéphane Wirtel - http://wirtel.be - @matrixise
History
Date User Action Args
2015-11-20 19:32:53matrixisesetmessages: + msg255012
2015-11-20 18:17:59serhiy.storchakasetmessages: + msg255005
2015-11-20 18:14:34serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg255004

resolution: rejected
stage: resolved
2015-11-20 17:47:05matrixisesetnosy: + matrixise
messages: + msg255003
2015-11-14 19:11:06Jáchym Barvíneksetmessages: + msg254667
2015-11-14 18:58:23desbmasetnosy: + desbma
messages: + msg254665
2015-11-14 17:46:56SilentGhostsetnosy: + pitrou

versions: + Python 3.6, - Python 3.5
2015-11-14 17:45:43Jáchym Barvínekcreate