classification
Title: pathlib.Path will be broken with os.chdir command.
Type: behavior Stage: resolved
Components: C API, Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Y Ono, eric.smith, pitrou, zach.ware
Priority: normal Keywords:

Created on 2020-05-20 00:50 by Y Ono, last changed 2020-05-20 06:17 by eric.smith. This issue is now closed.

Messages (4)
msg369410 - (view) Author: Y Ono (Y Ono) Date: 2020-05-20 00:50
```
import os
from pathlib import Path

os.chdir(Path(__file__).parent)
print(Path(__file__).absolute())
```
I put the code into a file in `/tmp/test.py`, and execute it from `/tmp/` directory like below. Then the output showed `/tmp/scripts/scripts/test.py`.

```
$ python scripts/test.py
/tmp/scripts/scripts/test.py
```


After executing `os.chdir`, pathlib's path handling is completely broken.
msg369411 - (view) Author: Y Ono (Y Ono) Date: 2020-05-20 00:58
It's the same as when executing `os.path.abspath`.
msg369418 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2020-05-20 06:09
I don't see anything broken here: `__file__` is "scripts/test.py", `Path(__file__).parent` is "scripts", and `__file__` doesn't change just because the working directory changed (it's a static string set at import time, which is only a relative path because you ran the file using a relative path).
msg369419 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-05-20 06:17
https://stackoverflow.com/questions/7116889/is-module-file-attribute-absolute-or-relative and https://bugs.python.org/issue18416 might help explain behavior. I agree it's working as designed.
History
Date User Action Args
2020-05-20 06:17:56eric.smithsetnosy: + eric.smith
messages: + msg369419
2020-05-20 06:09:13zach.waresetstatus: open -> closed

nosy: + zach.ware
messages: + msg369418

resolution: not a bug
stage: needs patch -> resolved
2020-05-20 05:10:36SilentGhostsetversions: + Python 3.7
nosy: + pitrou

components: + Library (Lib)
type: behavior
stage: needs patch
2020-05-20 00:58:34Y Onosetmessages: + msg369411
2020-05-20 00:50:18Y Onocreate