On this page
  1. os.scandir
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

os.scandir

Prev: os.rmdir | Up: os | Next: os.stat
Rel: os.abort, os.access, os.chdir, os.chmod
Ref: docs.python.org — os.scandir
Tags: #public #python3 #stdlib #os


Import

import os
from os import scandir

Signature & Overview

os.scandir(path=None)

Return an iterator of DirEntry objects for given path.

Usage

import os
from pathlib import Path
import tempfile

with tempfile.TemporaryDirectory() as tmp:
    (Path(tmp) / "a.py").write_text("x=1", encoding="utf-8")
    (Path(tmp) / "sub").mkdir()
    with os.scandir(tmp) as it:
        files = [e.name for e in it if e.is_file()]
    assert files == ["a.py"]

Gotchas

REPL Inspect

from os import scandir
help(scandir)

Linked from

Related notes