Python – List Files in a Directory

There are several modules available in Python to list files in a directory or folder. Some of the popular ones we can use are os, pathlib, glob, fnmatch, etc. This tutorial will look at the most popular way to list all the files in a directory.

Method 1: Using os.listdir() method

We can use os.listdir() to get all the files and directories in the specified path.

Syntax – os.listdir(path)

It takes a path as a parameter and returns a list of all files and directories in a specified path.

# import OS module
import os

# List all the files and directories
path = "C:\Projects\Tryouts"
dir_list = os.listdir(path)

print("Files and directories in '", path, "' :")

# prints all files
print(dir_list)

Output

Files and directories in ' C:\Projects\Tryouts ' :
['calc.py', 'etc', 'listindexerror.py', 'main.py', 'Python Tutorial.py', 'Python Tutorial.txt', 'test', 'test - Copy', '__pycache__']

Method 2: Using os.walk() method

The os module provides many functions to interact with operating system features, and one such method is os.walk(), which generates the files and folders in a directory tree. It can traverse the tree either top-down or bottom-up search, and by default, it sets as top-down search.

The os.walk() also helps to retrieve the files and folder in the absolute path.

# import OS module
import os

# List all the files and directories
path = "C:\Projects\Tryouts"
for (root, directories, files) in os.walk(path, topdown=False):
	for name in files:
		print(os.path.join(root, name))
	for name in directories:
		print(os.path.join(root, name))

Output

C:\Projects\Tryouts\etc\password.txt
C:\Projects\Tryouts\test\python.txt
C:\Projects\Tryouts\test - Copy\python.txt
C:\Projects\Tryouts\__pycache__\calc.cpython-39.pyc
C:\Projects\Tryouts\calc.py
C:\Projects\Tryouts\listindexerror.py
C:\Projects\Tryouts\main.py
C:\Projects\Tryouts\Python Tutorial.py
C:\Projects\Tryouts\Python Tutorial.txt
C:\Projects\Tryouts\etc
C:\Projects\Tryouts\test
C:\Projects\Tryouts\test - Copy
C:\Projects\Tryouts\__pycache__

Method 3: Using os.scan() method

os.scan() method is available in Python 3.5 and above. scandir() accepts either a bytes or str object for the path parameter and returns the DirEntry.name and DirEntry.path attributes with the same type as the path.

Syntax: os.scandir(path = ‘.’)

# import OS module
import os

# List all the files and directories
path = "C:\Projects\Tryouts"
data = os.scandir()

for item in data:
    if item.is_dir() or item.is_file():
        print(item.name)

Output

calc.py
etc
listindexerror.py
main.py
Python Tutorial.py
Python Tutorial.txt
test
test - Copy
__pycache__

Method 4: Using glob module

The glob module helps you retrieve the files/path matching a specified pattern as the glob supports the wildcard search. We can get both files and folders using the glob module.

# import OS module
import glob

# List all the files and directories
path = "C:\Projects\Tryouts\*"

for file_name in glob.iglob(path, recursive=True):
  print(file_name)
C:\Projects\Tryouts\calc.py
C:\Projects\Tryouts\etc
C:\Projects\Tryouts\listindexerror.py  
C:\Projects\Tryouts\main.py
C:\Projects\Tryouts\Python Tutorial.py 
C:\Projects\Tryouts\Python Tutorial.txt
C:\Projects\Tryouts\test
C:\Projects\Tryouts\test - Copy
C:\Projects\Tryouts\__pycache__

We can also print the filenames recursively using the iglob() method. All you need to do is set the recursive parameter as true.

Below the example, we use iglob() method with recursive set to true and searching with a specific pattern to get all the .py files

# import OS module
import glob

# List all the files and directories
path = "C:\Projects\Tryouts\*.py"

for file_name in glob.iglob(path, recursive=True):
  print(file_name)

Output

C:\Projects\Tryouts\calc.py
C:\Projects\Tryouts\listindexerror.py 
C:\Projects\Tryouts\main.py
C:\Projects\Tryouts\Python Tutorial.py

0 Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…