TryBugHunt

TypeError: 'WindowsPath' object is not iterable

TypeError: 'WindowsPath' object is not iterable in Python means you looped over a pathlib Path object as if it were a list of files or a list of lines. A Path is just one address on disk, so Python cannot loop over it. On Mac and Linux the same error says 'PosixPath'.

PythonPattern: Type errors

How do I fix TypeError: 'WindowsPath' object is not iterable?

TypeError: 'WindowsPath' object is not iterable means you looped over a pathlib Path as if it were a list. A Path only stores a location. To loop over files in a folder, use path.iterdir() or path.glob("*.csv"). To loop over lines in a file, use with path.open() as f. On Mac and Linux it says 'PosixPath'.

  1. 1. Print the loop valuePrint the value and type(value). If it is a Path, it is one location, not a collection.
  2. 2. Folder or file?For a folder use .iterdir() or .glob(). For a file use .open() or .read_text().splitlines().
  3. 3. Wrap single pathsIf a function expects many paths, pass [path] or a .glob() result, not one Path.

What does TypeError: 'WindowsPath' object is not iterable mean?

Path("data") or Path("notes.txt") only stores a location. It does not open the file or read the folder. To loop over the files in a folder, ask for them with .iterdir() or .glob(). To loop over the lines in a file, open it or read its text first.

Why does TypeError: 'WindowsPath' object is not iterable happen?

1. Looping over a folder Path to get its files

A folder Path does not hold its files. .iterdir() lists everything inside it, and .glob("*.csv") lists only matching files.

Breaks

from pathlib import Path

for f in Path("data"):
    print(f.name)

Works

from pathlib import Path

for f in Path("data").iterdir():
    print(f.name)

2. Looping over a file Path to get its lines

The file has not been opened. Open it, or read its text and split it into lines.

Breaks

from pathlib import Path

for line in Path("notes.txt"):
    print(line)

Works

from pathlib import Path

with Path("notes.txt").open() as f:
    for line in f:
        print(line.rstrip())

3. Passing one Path where a list of paths is expected

A function that loops over its argument needs a list. Wrap a single path in a list, or pass the result of .glob().

Breaks

def total_size(paths):
    return sum(p.stat().st_size for p in paths)

total_size(Path("report.pdf"))

Works

def total_size(paths):
    return sum(p.stat().st_size for p in paths)

total_size([Path("report.pdf")])

How to find it in your own code

Print the value you are looping over and its type. If it is a Path, decide what you meant: the files in a folder (.iterdir() or .glob()), or the lines of a file (open it, or .read_text().splitlines()).

Practise type errors

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all type errors →