-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pathlib: Path('.').exists() returns True when current working directory (cwd) was deleted #127264
Comments
I believe this is reproducible with |
On unix systems when the current working directory (CWD) is removed the directory still exists, but is "just" unlinked from the directory tree and marked as unmodifiable (as in, you cannot create new entries in the directory). So, IMHO
The API documentation for the |
I can see that both of those things have logical reasons for being true, but it is very confusing as a user when you write something like: dot_path = Path(".")
if dot_path.exists():
dot_path = dot_path.resolve() and it raises a |
Checking before resolving isn't good enough in general, the filesystem might change between the check and the call to resolve. This is a well known source of security issues in other use cases (although that shouldn't be relevant here). |
Bug report
Bug description:
Path.exists()
andPath.resolve()
do not work consistently with each other if the current working directory is deleted after Python is launched.For example, if I open a Python 3.13.0 interpreter (although I have verified this occurs in older versions as well) in a directory, and then delete that directory I experience the following behavior:
Note how
Path('.').exists()
isTrue
, even though the cwd does not exist, and then when I try to resolve the path, I get aFileNotFoundError
.You also get a
FileNotFoundError
when the cwd doesn't exist for all relative paths, even though in the non-dot casesPath.exists()
works as one would expect.If the cwd does exist, you can see that
Path.resolve()
does not raise aFileNotFoundError
when the path does not exist.I believe resolve is doing the right thing here, but two things are not working as expected when the cwd is deleted:
Path('.').exists()
andPath('').exists()
should not tell you a path exists that doesn't.Path('foo').resolve()
should not raise aFileNotFoundError
if the cwd doesn't exist, unlessstrict=True
. Instead it should just return the initial path.CPython versions tested on:
3.9, 3.12, 3.13
Operating systems tested on:
Linux, macOS
The text was updated successfully, but these errors were encountered: