`os.walk()` should default to the current dir

os.walk() requires a top argument, and if you want the current dir, you need to specify . i.e. os.walk("."). However, os.fwalk() has a default top='.', so why doesn’t .walk()?

This would make it more convenient to walk the current dir.

This would also be good for comparison with os.listdir(), which has a default path='.', and is also used for listing files (similar to ls and find in Unix).

To be clear, I’m proposing changing def walk(top, ...) to def walk(top=".", ...) in os.py.
Cf def fwalk(top=".", ...)

Notes:

  • os.fwalk() was added in 3.3
  • os.listdir()'s path default was added in 3.2.
8 Likes

This strikes me as a sensible request to put on the issues tracker.

2 Likes

It is supposed to pass dirfd to os.fwalk(). In that case top='.' is not the current directory, but the directory specified by dirfd. It is reasonable to make it default, so you do not need to pass top='.' each time you use dirfd.

In os.walk(), top='.' is not the most common case.

@storchaka Minor correction: the parameter to fwalk is called dir_fd (with an underscore)

Ah, I see, but still, what I’m saying is, fwalk() is a valid call, while walk() isn’t. So there’s an asymmetry there, you see?

Alternatively, you could make fwalk() not a valid call by requiring top if dir_fd isn’t specified, like this:

def fwalk(top=None, ..., dir_fd=None):
    if dir_fd is None and top is None:
        raise TypeError('At least one of "dir_fd" and "top" must be provided.')
    if top is None:
        top = '.'