1. Antoine Pitrou
  2. pathlib
  3. Issues
Issue #24 new

with context for chdir

created an issue

When working with external commands it is quite common to change cwd, because many external command-line programs uses cwd for determining paths.

For this, I think that pathlib should provide with context, for temporary changing cwd. path.py provides this functionality like this:

with p / 'other':

Comments (3)

  1. Antoine Pitrou repo owner

    Ha... sorry for answering a bit late here (I wonder if bitbucket sent me an e-mail notification for this). There are two factors making me lukewarm here:

    1. changing the current directory is not a recommended practice, because it's a process-global setting; it's ok in scripts but not in libraries or complex (especially multi-threaded) applications
    2. there's no obvious reason using a path as a context manager should implicitly chdir() to that path, rather than any other action (actually, a much earlier version of pathlib used it in conjuction with openat() to manage the lifetime of the underlying fd)
  2. sirex reporter

    Antoine Pitrou I agree with you that chdir() should be used with care. And at the time, when I was writing this issue, I was writing a script, where I had to implement context manager myself. I found, that in scripts, this context manager is very useful thing.

    So I think, that there are use cases, where such context manager would be very useful.

  3. Björn Pollex

    I would very much appreciate if this was part of the library, maybe with a more explicit syntax than what the OP proposed. I am routinely adding a small snippet for this to scripts. Even if it is bad practice in certain situations, I'd argue that programmer should choose for themselves.

  4. Log in to comment