Python pathlib Path.info

Python 3.14 adds Path.info to pathlib. It exposes file-type information associated with a Path and can reuse metadata learned while iterating through a directory.

Inspect a Path

Example:

from pathlib import Path

path = Path("report.txt")

print(path.info.is_file())
print(path.info.is_dir())

The output depends on whether report.txt exists. For the directory examples below, replace uploads and media with existing directories; otherwise iterdir() raises FileNotFoundError.

Use It with iterdir()

Path.info is especially useful with paths returned by directory iteration because the iterator may already know file-type information from the operating system.

Example:

from pathlib import Path

folder = Path("uploads")

for entry in folder.iterdir():
    if entry.info.is_file():
        print("File:", entry.name)
    elif entry.info.is_dir():
        print("Folder:", entry.name)

Why It Can Be Faster

Calling methods such as is_file() directly on many paths may require repeated filesystem metadata operations. Cached information can reduce that work when the operating system already supplied it.

Cached Information Can Become Stale

The cached information may become stale. Use path.is_file() or path.is_dir() for a fresh query. To start with an empty info cache, create another path object: fresh = Path(path). Merely accessing path.info does not query the filesystem.

Approach Best fit
Path.info Efficient classification during directory processing
Path.is_file() Fresh direct query when current state matters
Path.is_dir() Fresh directory check

Practical Directory Filter

The following filter reads names only; it does not open image files or guarantee that the files remain unchanged afterward.

Example:

from pathlib import Path

images = [
    path
    for path in Path("media").iterdir()
    if path.info.is_file()
    and path.suffix.lower() in {".png", ".jpg"}
]

for image in images:
    print(image.name)

Use cached path information for discovery and bulk directory traversal, but use fresh queries for correctness-sensitive checks after the filesystem may have changed.

Conclusion

Path.info gives Python 3.14 pathlib code a convenient way to reuse file-type metadata. It is most valuable when classifying many entries returned by directory iteration.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram