Skip to main content
scandir on PyPI (Python Package Index) Travis CI tests (Linux) Appveyor tests (Windows)

scandir() is a directory iteration function like os.listdir(), except that instead of returning a list of bare filenames, it yields DirEntry objects that include file type and stat information along with the name. Using scandir() increases the speed of os.walk() by 2-20 times (depending on the platform and file system) by avoiding unnecessary calls to os.stat() in most cases.

Now included in a Python near you!

scandir has been included in the Python 3.5 standard library as os.scandir(), and the related performance improvements to os.walk() have also been included. So if you’re lucky enough to be using Python 3.5 (release date September 13, 2015) you get the benefit immediately, otherwise just download this module from PyPI, install it with pip install scandir, and then do something like this in your code:

# Use the built-in version of scandir/walk if possible, otherwise
# use the scandir module version
try:
    from os import scandir, walk
except ImportError:
    from scandir import scandir, walk

PEP 471, which is the PEP that proposes including scandir in the Python standard library, was accepted in July 2014 by Victor Stinner, the BDFL-delegate for the PEP.

This scandir module is intended to work on Python 2.7+ and Python 3.4+ (and it has been tested on those versions).

Background

Python’s built-in os.walk() is significantly slower than it needs to be, because – in addition to calling listdir() on each directory – it calls stat() on each file to determine whether the filename is a directory or not. But both FindFirstFile / FindNextFile on Windows and readdir on Linux/OS X already tell you whether the files returned are directories or not, so no further stat system calls are needed. In short, you can reduce the number of system calls from about 2N to N, where N is the total number of files and directories in the tree.

In practice, removing all those extra system calls makes os.walk() about 7-50 times as fast on Windows, and about 3-10 times as fast on Linux and Mac OS X. So we’re not talking about micro-optimizations. See more benchmarks in the “Benchmarks” section below.

Somewhat relatedly, many people have also asked for a version of os.listdir() that yields filenames as it iterates instead of returning them as one big list. This improves memory efficiency for iterating very large directories.

So as well as a faster walk(), scandir adds a new scandir() function. They’re pretty easy to use, but see “The API” below for the full docs.

Benchmarks

Below are results showing how many times as fast scandir.walk() is than os.walk() on various systems, found by running benchmark.py with no arguments:

System version

Python version

Times as fast

Windows 7 64-bit

2.7.7 64-bit

10.4

Windows 7 64-bit SSD

2.7.7 64-bit

10.3

Windows 7 64-bit NFS

2.7.6 64-bit

36.8

Windows 7 64-bit SSD

3.4.1 64-bit

9.9

Windows 7 64-bit SSD

3.5.0 64-bit

9.5

Ubuntu 14.04 64-bit

2.7.6 64-bit

5.8

Mac OS X 10.9.3

2.7.5 64-bit

3.8

All of the above tests were done using the fast C version of scandir (source code in _scandir.c).

Note that the gains are less than the above on smaller directories and greater on larger directories. This is why benchmark.py creates a test directory tree with a standardized size.

The API

walk()

The API for scandir.walk() is exactly the same as os.walk(), so just read the Python docs.

scandir()

The full docs for scandir() and the DirEntry objects it yields are available in the Python documentation here. But below is a brief summary as well.

scandir(path=’.’) -> iterator of DirEntry objects for given path

Like listdir, scandir calls the operating system’s directory iteration system calls to get the names of the files in the given path, but it’s different from listdir in two ways:

  • Instead of returning bare filename strings, it returns lightweight DirEntry objects that hold the filename string and provide simple methods that allow access to the additional data the operating system may have returned.

  • It returns a generator instead of a list, so that scandir acts as a true iterator instead of returning the full list immediately.

scandir() yields a DirEntry object for each file and sub-directory in path. Just like listdir, the '.' and '..' pseudo-directories are skipped, and the entries are yielded in system-dependent order. Each DirEntry object has the following attributes and methods:

  • name: the entry’s filename, relative to the scandir path argument (corresponds to the return values of os.listdir)

  • path: the entry’s full path name (not necessarily an absolute path) – the equivalent of os.path.join(scandir_path, entry.name)

  • is_dir(*, follow_symlinks=True): similar to pathlib.Path.is_dir(), but the return value is cached on the DirEntry object; doesn’t require a system call in most cases; don’t follow symbolic links if follow_symlinks is False

  • is_file(*, follow_symlinks=True): similar to pathlib.Path.is_file(), but the return value is cached on the DirEntry object; doesn’t require a system call in most cases; don’t follow symbolic links if follow_symlinks is False

  • is_symlink(): similar to pathlib.Path.is_symlink(), but the return value is cached on the DirEntry object; doesn’t require a system call in most cases

  • stat(*, follow_symlinks=True): like os.stat(), but the return value is cached on the DirEntry object; does not require a system call on Windows (except for symlinks); don’t follow symbolic links (like os.lstat()) if follow_symlinks is False

  • inode(): return the inode number of the entry; the return value is cached on the DirEntry object

Here’s a very simple example of scandir() showing use of the DirEntry.name attribute and the DirEntry.is_dir() method:

def subdirs(path):
    """Yield directory names not starting with '.' under given path."""
    for entry in os.scandir(path):
        if not entry.name.startswith('.') and entry.is_dir():
            yield entry.name

This subdirs() function will be significantly faster with scandir than os.listdir() and os.path.isdir() on both Windows and POSIX systems, especially on medium-sized or large directories.

Further reading

  • The Python docs for scandir

  • PEP 471, the (now-accepted) Python Enhancement Proposal that proposed adding scandir to the standard library – a lot of details here, including rejected ideas and previous discussion

Flames, comments, bug reports

Please send flames, comments, and questions about scandir to Ben Hoyt:

http://benhoyt.com/

File bug reports for the version in the Python 3.5 standard library here, or file bug reports or feature requests for this module at the GitHub project page:

https://github.com/benhoyt/scandir

Release files for scandir 1.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scandir 1.10.0
File Size Uploaded
scandir-1.10.0.tar.gz 33.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for scandir 1.10.0
File
scandir-1.10.0-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
scandir-1.10.0-cp37-cp37m-win32.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-32 Details
scandir-1.10.0-cp36-cp36m-win_amd64.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-64 Details
scandir-1.10.0-cp36-cp36m-win32.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-32 Details
scandir-1.10.0-cp35-cp35m-win_amd64.whl CPython 3.5 CPython 3.5 pymalloc Windows x86-64 Details
scandir-1.10.0-cp35-cp35m-win32.whl CPython 3.5 CPython 3.5 pymalloc Windows x86-32 Details
scandir-1.10.0-cp34-cp34m-win_amd64.whl CPython 3.4 CPython 3.4 pymalloc Windows x86-64 Details
scandir-1.10.0-cp34-cp34m-win32.whl CPython 3.4 CPython 3.4 pymalloc Windows x86-32 Details
scandir-1.10.0-cp27-cp27m-win_amd64.whl CPython 2.7 CPython 2.7 pymalloc Windows x86-64 Details
scandir-1.10.0-cp27-cp27m-win32.whl CPython 2.7 CPython 2.7 pymalloc Windows x86-32 Details

Total release size: 250.2 kB

Release files / scandir-1.10.0.tar.gz

Download URL scandir-1.10.0.tar.gz
Size 33.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4d4631f6062e658e9007ab3149a9b914f3548cb38bfb021c64f39a025ce578ae
BLAKE2b-256 checksum
How to use checksums
dff59c052db7bd54d0cbf1bc0bb6554362bba1012d03e5888950a4f5c5dadc4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp37-cp37m-win_amd64.whl

Download URL scandir-1.10.0-cp37-cp37m-win_amd64.whl
Size 22.8 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
b24086f2375c4a094a6b51e78b4cf7ca16c721dcee2eddd7aa6494b42d6d519d
BLAKE2b-256 checksum
How to use checksums
cb06cee31a831784ae66073fff7df58f823a1f6bc3947dc48a76f877b17eb52b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp37-cp37m-win32.whl

Download URL scandir-1.10.0-cp37-cp37m-win32.whl
Size 22.1 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
67f15b6f83e6507fdc6fca22fedf6ef8b334b399ca27c6b568cbfaa82a364173
BLAKE2b-256 checksum
How to use checksums
9b474bf2941582c731b0c3a7aee7a3129063fe7dad5382cd662f3766dac619b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp36-cp36m-win_amd64.whl

Download URL scandir-1.10.0-cp36-cp36m-win_amd64.whl
Size 22.8 kB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
7d2d7a06a252764061a020407b997dd036f7bd6a175a5ba2b345f0a357f0b3f4
BLAKE2b-256 checksum
How to use checksums
a0c38b8244553f4cf8682825e46d264f0bf3b8f7a51c9ba4745c8aa9182da4e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp36-cp36m-win32.whl

Download URL scandir-1.10.0-cp36-cp36m-win32.whl
Size 22.1 kB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
2ae41f43797ca0c11591c0c35f2f5875fa99f8797cb1a1fd440497ec0ae4b022
BLAKE2b-256 checksum
How to use checksums
25c5257e7f38127de5221a57e6afd0eb6ad0a85412c92644bf8265f20085b22a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp35-cp35m-win_amd64.whl

Download URL scandir-1.10.0-cp35-cp35m-win_amd64.whl
Size 22.8 kB
Tags CPython 3.5 CPython 3.5 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
8c5922863e44ffc00c5c693190648daa6d15e7c1207ed02d6f46a8dcc2869d32
BLAKE2b-256 checksum
How to use checksums
b75dc0dc3933bd79fdca4780f84fffb2291672f3db8f0093c5d2ce629b7cb656
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp35-cp35m-win32.whl

Download URL scandir-1.10.0-cp35-cp35m-win32.whl
Size 22.1 kB
Tags CPython 3.5 CPython 3.5 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
2b8e3888b11abb2217a32af0766bc06b65cc4a928d8727828ee68af5a967fa6f
BLAKE2b-256 checksum
How to use checksums
cda1d5c3e22090ebead6d24abbe0f85641f002db44316c0a422d68d2fa258a8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp34-cp34m-win_amd64.whl

Download URL scandir-1.10.0-cp34-cp34m-win_amd64.whl
Size 20.5 kB
Tags CPython 3.4 CPython 3.4 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
2586c94e907d99617887daed6c1d102b5ca28f1085f90446554abf1faf73123e
BLAKE2b-256 checksum
How to use checksums
773a7f6111552b4736a08a72c37d6b8852cf77c02042a3124a8806f67008ad45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp34-cp34m-win32.whl

Download URL scandir-1.10.0-cp34-cp34m-win32.whl
Size 20.2 kB
Tags CPython 3.4 CPython 3.4 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
2c712840c2e2ee8dfaf36034080108d30060d759c7b73a01a52251cc8989f11f
BLAKE2b-256 checksum
How to use checksums
0486417b435bd94a4e0176c10acea5b866a47a045848a346c6930fc846b2f016
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp27-cp27m-win_amd64.whl

Download URL scandir-1.10.0-cp27-cp27m-win_amd64.whl
Size 20.9 kB
Tags CPython 2.7 CPython 2.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
cb925555f43060a1745d0a321cca94bcea927c50114b623d73179189a4e100ac
BLAKE2b-256 checksum
How to use checksums
f9d06b7b38eaf9964510f5c32aa5aaf9f419864d2e0ebe34274e6cba5689a0c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release files / scandir-1.10.0-cp27-cp27m-win32.whl

Download URL scandir-1.10.0-cp27-cp27m-win32.whl
Size 20.5 kB
Tags CPython 2.7 CPython 2.7 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
92c85ac42f41ffdc35b6da57ed991575bdbe69db895507af88b9f499b701c188
BLAKE2b-256 checksum
How to use checksums
c68c43cc3799c79c435d1a236783993b2e04a2c750b4f91ef3630ec442490df5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

Release history Release notifications | RSS feed

This release

1.10.0 This release

11 release files

1.9.0

11 release files

1.8

11 release files

1.7

11 release files

1.6

11 release files

1.5

1 release file

1.4

2 release files

1.3

2 release files

1.2

3 release files

1.1

3 release files

1.0

3 release files

0.9

3 release files

0.8

3 release files

0.6

3 release files

0.5

3 release files

0.4

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page