pythonpathos.walk

Why do i get a `'PosixPath' object has no attribute 'walk'` exception?


Why do i get a 'PosixPath' object has no attribute 'walk' exception?

from pathlib import Path
p = Path()
for dirpath, dirnames, filenames in p.walk():
    print(dirpath, dirnames, filenames)

Solution

  • Path.walk was added in Python 3.12, so you propably using an older version.

    Instead, use os.walk or Path.glob() (or update your Python)

    You can check the version in python with:

    import sys
    print(sys.version)