Skip to content

os.walk() silent about non-directory #101420

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

Open
serhiy-storchaka opened this issue Jan 30, 2023 · 2 comments
Open

os.walk() silent about non-directory #101420

serhiy-storchaka opened this issue Jan 30, 2023 · 2 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

os.walk() silently produces an empty iterator if the argument is not a path to directory:

>>> import os
>>> list(os.walk('./non-existing'))
[]
>>> list(os.walk('./python'))
[]
>>> list(os.walk('/dev/null'))
[]

It can causes bugs like #99203, when a function produces some incorrect result instead of raising an exception.

@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Jan 30, 2023
@serhiy-storchaka
Copy link
Member Author

Note that os.fwalk() raises an exception for non-existing entity, but not for file.

>>> list(os.fwalk('./non-existing'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/os.py", line 475, in fwalk
    orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: './non-existing'
>>> list(os.fwalk('./python'))
[]

glob() also produces an empty result:

>>> glob.glob('./non-existing/*')
[]
>>> glob.glob('./python/*')
[]

@achhina
Copy link
Contributor

achhina commented Mar 10, 2023

I would like to work on this.

@serhiy-storchaka what do you think the expected behavior for this should be? Looking at the docs, it seems to imply os.fwalk should mimic os.walk, however I agree that silently producing an iterator is probably not the best way to deal with this. Also, would there be any backward compatibility concerns for folks who are relying on this feature in os.walk, if we decide to modify it?

@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants