How to find files and skip directories in os.listdir

You need to filter out directories; os.listdir() lists all names in a given path. You can use os.path.isdir() for this:

basepath = '/path/to/directory'
for fname in os.listdir(basepath):
    path = os.path.join(basepath, fname)
    if os.path.isdir(path):
        # skip directories
        continue

Note that this only filters out directories after following symlinks. fname is not necessarily a regular file, it could also be a symlink to a file. If you need to filter out symlinks as well, you'd need to use not os.path.islink() first.

On a modern Python version (3.5 or newer), an even better option is to use the os.scandir() function; this produces DirEntry() instances. In the common case, this is faster as the direntry loaded already has cached enough information to determine if an entry is a directory or not:

basepath = '/path/to/directory'
for entry in os.scandir(basepath):
    if entry.is_dir():
        # skip directories
        continue
    # use entry.path to get the full path of this entry, or use
    # entry.name for the base filename

You can use entry.is_file(follow_symlinks=False) if only regular files (and not symlinks) are needed.

os.walk() does the same work under the hood; unless you need to recurse down subdirectories, you don't need to use os.walk() here.


Here is a nice little one-liner in the form of a list comprehension:

[f for f in os.listdir(your_directory) if os.path.isfile(os.path.join(your_directory, f))]

This will return a list of filenames within the specified your_directory.