Introduction
Listing files in a directory is a common task in file management. Python provides several ways to list files in a directory, including the os
module, the os.path
module, and the glob
module. Each method has its advantages and can be used depending on your specific needs.
Using the os Module
The os
module provides a portable way of using operating system-dependent functionality, including listing files in a directory.
os.listdir()
The os.listdir()
function returns a list containing the names of the entries in the directory given by path
.
Example
import os
# List files in the current directory
files = os.listdir('.')
print(files)
Output
['file1.txt', 'file2.txt', 'directory1', 'script.py']
Filtering Files
You can filter the list to include only files and exclude directories.
import os
# List files in the current directory, excluding directories
files = [f for f in os.listdir('.') if os.path.isfile(os.path.join('.', f))]
print(files)
Output
['file1.txt', 'file2.txt', 'script.py']
Using os.walk()
The os.walk()
function generates the file names in a directory tree by walking either top-down or bottom-up.
Example
import os
# List all files in a directory tree
for root, dirs, files in os.walk('.'):
for file in files:
print(os.path.join(root, file))
Output
./file1.txt
./file2.txt
./script.py
./directory1/file3.txt
Using the glob Module
The glob
module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell. It can be used to list files with specific patterns.
Example
import glob
# List all .txt files in the current directory
txt_files = glob.glob('*.txt')
print(txt_files)
Output
['file1.txt', 'file2.txt']
Recursive Listing
You can use the glob
module to list files recursively by using the **
pattern.
import glob
# List all .txt files in the current directory and subdirectories
txt_files = glob.glob('**/*.txt', recursive=True)
print(txt_files)
Output
['file1.txt', 'file2.txt', 'directory1/file3.txt']
Using the pathlib Module
The pathlib
module provides an object-oriented approach for working with filesystem paths. It offers an easy way to list files in a directory.
Example
from pathlib import Path
# List files in the current directory
files = [f for f in Path('.').iterdir() if f.is_file()]
print(files)
Output
[PosixPath('file1.txt'), PosixPath('file2.txt'), PosixPath('script.py')]
Filtering by Extension
You can filter files by their extension using the glob
method from pathlib
.
from pathlib import Path
# List all .txt files in the current directory
txt_files = list(Path('.').glob('*.txt'))
print(txt_files)
Output
[PosixPath('file1.txt'), PosixPath('file2.txt')]
Recursive Listing
You can list files recursively by using the rglob
method.
from pathlib import Path
# List all .txt files in the current directory and subdirectories
txt_files = list(Path('.').rglob('*.txt'))
print(txt_files)
Output
[PosixPath('file1.txt'), PosixPath('file2.txt'), PosixPath('directory1/file3.txt')]
Conclusion
Listing files in Python can be done using various modules and methods. The os
module provides basic functionality for listing files and directories, while the glob
module offers pattern matching capabilities. The pathlib
module provides an object-oriented approach for handling filesystem paths and can be used for both simple and recursive listings. Understanding these methods allows you to efficiently manage and process files in Python applications.