
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate Over Files in a Given Directory in Python
Iterating over files in a given directory helps perform tasks such as finding files that match certain criteria or, counting the number of files in a directory. Python provides the following five ways to walk through all the existing files in a directory -
- os.listdir() method
- os.walk() method
- os.scandir() method
- Using pathlib module
- glob.iglob() method
In this article, we are going to see all the above methods that are used to iterate over files in a given directory in Python.
Using os.listdir() Method
The os.listdir() method is used to list all the files present in a directory. It accepts the path of the directory as an argument and all the entries, apart from special entries such as "." and ".." are returned as a list.
Following is the syntax of this method -
os.listdir(path)
Example
In the following example, we are trying to list all the files present in the current directory using the method os.listdir() -
import os, sys path = r"D:\Tutorialspoint\sample" dir = os.listdir( path ) for file in dir: print(file)
Following is the output of the above program -
folder1 folder2
Using os.walk() Method
The os.walk() method generates file names in a directory tree by walking it top-down or bottom-up approach. It returns a three-tuple for each directory in the tree rooted at directory top as path, names, and filenames.
Example
Following is the example in which we use the os.walk() method within a loop statement to display all the files and subdirectories present in the current root directory -
import os path = r"D:\Tutorialspoint\sample" for root, d_names, f_names in os.walk(path): print(root, d_names, f_names)
Below is the output of the above program -
D:\Tutorialspoint\sample ['folder1', 'folder2'] [] D:\Tutorialspoint\sample\folder1 [] ['sample1.txt', 'sample2.txt'] D:\Tutorialspoint\sample\folder2 [] ['sample.txt']
Example
In the below example, we are using the os.walk() method, in which we choose to display what element of the return value tuple, and we want to print -
import os for dirpath, dirs, files in os.walk("D:\Tutorialspoint\sample"): print(dirpath) # prints paths of all subdirectories present for dirpath, dirs, files in os.walk("D:\Tutorialspoint\sample"): print(dirs) # prints the names of existing subdirectories for dirpath, dirs, files in os.walk("D:\Tutorialspoint\sample"): print(files) # prints existing files in the current directory
Following is the output of the above code -
. ['folder1', 'folder2'] [] [] [] ['sample1.txt', 'sample2.txt'] ['sample.txt']
Using os.listdir() Method
The os.listdir() method is useful to get us everything that's present in the given directory, including both files and sub-directories. We can have to use this method with a loop statement to list all the files and sub-directories present in a directory.
Example
Following is the example, in which we will try to use the os.listdir() method in a loop statement to iterate through all the files present in a directory -
import os path = "." for file_names in os.listdir(path): print(file_names)
Here is the output of the above program -
folder1 folder2
Using the pathlib Module
The pathlib module provides classes representing the filesystem paths. It is similar to the path module, but the path module creates a string to represent a file path, while the pathlib module creates objects. In this module, we use the glob() method to list files and subdirectories present in a directory.
Example
Here in this example, we will print the names of all the files and subdirectories present in the root directory using the glob() method -
from pathlib import Path root_directory = Path('.') size = 0 for f in root_directory.glob("*"): print(f)
Following is the output of the above example -
D:\Tutorialspoint\sample\folder1 D:\Tutorialspoint\sample\folder2
Using the glob Module
In Python, we have the glob module with iglob() method, which is used to search for files in a directory. It uses a pattern and matches it with the files present in a directory. If the files are considered a match with the pattern, then they will be listed.
Example
In this example, we are using the iglob() method to list all the files in the current directory and iterate over the files -
import glob pattern = r"D:\Tutorialspoint\sample" for f in glob.iglob(pattern): print(f)
Here is the output of the above program -
D:\Tutorialspoint\sample