Get List Of Files In A Directory Python

4 min read

Getting a list of files in a directory is one of the most common tasks in Python, especially when building file processors, backup tools, scrapers, or automation scripts. Whether you need to read every image in a folder, process CSV files, or clean up old logs, knowing how to get a list of files in a directory in Python is an essential skill. This guide explains the most practical methods, from simple one-liners to more strong patterns, so you can choose the right approach for your project Worth keeping that in mind..

Introduction

When working with files, Python offers several built-in modules that make directory operations straightforward. The most common tools are os, os.Here's the thing — path, pathlib, and glob. Practically speaking, each one has a slightly different style and purpose. Some are better for quick scripts, while others are better for production applications.

A typical task might be as simple as:

  • List all files in the current folder.
  • List only files with a specific extension.
  • Include subfolders or exclude them.
  • Sort the results by name or modification time.
  • Handle missing directories safely.

The best method depends on what you need. For beginners, os.Still, listdir() is easy to understand. For modern Python code, pathlib is often cleaner and more readable. For pattern matching, glob and pathlib are very useful Small thing, real impact..

The Simplest Method: os.listdir()

The fastest way to get the names of files and folders in a directory is to use os.So listdir(). It returns a list of names, but it does not tell you whether each item is a file or a directory.

import os

directory = "/path/to/folder"
items = os.listdir(directory)

for item in items:
    print(item)

This code prints every item inside the directory, including files and subdirectories.

If you want only files, you need to check each item with os.path.isfile().

import os

directory = "/path/to/folder"
files = [item for item in os.Which means path. Still, listdir(directory) if os. Because of that, isfile(os. path.

for file_name in files:
    print(file_name)

This is a very common pattern. It gives you a clean list of files only Small thing, real impact..

When to Use os.listdir()

Use os.listdir() when:

  • You want a quick and simple solution.
  • You are writing a small script.
  • You do not need advanced path handling.
  • You are comfortable working with string paths.

That said, os.listdir() does not provide full path objects, so you often need to join paths manually And that's really what it comes down to..

A Cleaner Approach: pathlib.Path

In modern Python, pathlib is often preferred because it makes path operations more readable. It represents paths as objects instead of plain strings.

from pathlib import Path

directory = Path("/path/to/folder")
files = [item.Practically speaking, name for item in directory. iterdir() if item.

for file_name in files:
    print(file_name)

This code does the same thing as the os example, but it is easier to read and less error-prone Simple, but easy to overlook..

If you want full paths instead of just names:

from pathlib import Path

directory = Path("/path/to/folder")
files = [item for item in directory.iterdir() if item.is_file()]

for file_path in files:
    print(file_path)

Why pathlib Is Often Better

pathlib is useful because it lets you do things like:

  • Check if a path is a file or directory.
  • Get the file name, suffix, and stem.
  • Join paths without manual string handling.
  • Write code that is easier to maintain.

For example:

from pathlib import Path

directory = Path("/path/to/folder")

for item in directory.iterdir():
    if item.is_file():
        print(item.name)
        print(item.suffix)

This is much cleaner than working with raw strings Nothing fancy..

Getting Only Files With a Specific Extension

Many real-world tasks require filtering files by type. To give you an idea, you may want only .Also, txt, . csv, or .jpg files.

Using pathlib, this is simple:

from pathlib import Path

directory = Path("/path/to/folder")
files = [item for item in directory.Here's the thing — is_file() and item. Plus, iterdir() if item. suffix == ".

for file_path in files:
    print(file_path)

If you want multiple extensions:

from pathlib import Path

directory = Path("/path/to/folder")
allowed_extensions = {".txt", ".csv", ".log"}

files = [item for item in directory.Because of that, iterdir() if item. is_file() and item.

for file_path in files:
    print(file_path)

This pattern is very useful when processing only relevant files and ignoring others.

Using glob for Pattern Matching

The glob module is useful when you want to match files using patterns.

import glob

pattern = "/path/to/folder/*.txt"
files = glob.glob(pattern)

for file_path in files:
    print(file_path)

You can also match multiple patterns:

import glob

files = glob.glob("/path/to/folder/*.csv")
files += glob.glob("/path/to/folder/*.json")

for file_path in files:
    print(file_path)

glob is especially helpful when you want to match file names with wildcards.

glob vs `path

Just Made It Online

Straight to You

Related Territory

Keep Exploring

Thank you for reading about Get List Of Files In A Directory Python. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home