- Python Find Files with Extension (.txt): A Step-by-Step Guide
- 3 Ways to Find All Files by Extension in Python
- 1. The ‘glob’ Module
- Example
- 2. Recursive Search with ‘os.listdir()’
- Example
- 3. The ‘os.walk’ Function
- Example
- Summary
- Read Also
- How To Find files with Certain Extension using Python
- Example: Finding files in a directory using the listdir() Function
- Example: Finding file using the endswith() Function
- Example: Finding files with a certain extension using the comprehension
- Example: Finding files with a certain extension using the glob and os Module
- Conclusion
Python Find Files with Extension (.txt): A Step-by-Step Guide
To find all files with a particular extension in Python, you can use the os.walk function to traverse the directory structure, and check if each file has the desired extension using the file.endswith() method.
For example, let’s find all the .txt files:
import os for root, dirs, files in os.walk('/path/to/directory'): for file in files: if file.endswith('.txt'): print(os.path.join(root, file))
This will print the full path of all files with the .txt extension in the specified directory. You can replace the .txt with any other desired file extension.
If you’re looking for a quick answer, I’m sure the above will do!
But if you’re looking to improve your Python skills and want to learn how to recursively find files in a directory with subdirectories, keep on reading. This guide introduces you to 3 different methods you can use to find files of a particular type in Python.
Let’s jump into it!
3 Ways to Find All Files by Extension in Python
There are three main ways to find all the files by extension in Python.
Let’s take a closer look at how each of these approaches works.
1. The ‘glob’ Module
You can use the glob module in Python to find all files with the .txt extension using the following code:
import glob # Find all files with the .txt extension in the current directory txt_files = glob.glob('*.txt') # Print the names of the files found print(txt_files)
The glob.glob() function searches for all files in the current directory that match the specified pattern, which in this case is ‘*.txt’ . This tells the function to find all files with a name that ends in .txt . The result is a list of the names of the matching files.
You can also specify a different directory to search in by passing the path to the directory as the first argument to the glob.glob() function. For example, if you want to search for all .txt files in the /path/to/directory directory, run the following:
import glob # Find all files with the .txt extension in the specified directory txt_files = glob.glob('/path/to/directory/*.txt') # Print the names of the files found print(txt_files)
Example
Here’s an example project I have:
The above image shows the folder structure, but in case you need clarity, here’s what it looks like:
Let’s find all the text files (ending with ‘.txt’) in the folder.
To do this, let’s run the code from the previous section to see what happens:
import glob # Find all files with the .txt extension in the current directory txt_files = glob.glob('*.txt') # Print the names of the files found print(txt_files)
It returns the ‘ names.txt ‘ and ‘ values.txt ‘ files but not the text files in the subfolders. To list the files with a particular extension in the subfolders, you need to take a recursive approach.
2. Recursive Search with ‘os.listdir()’
As you saw in the earlier example, the glob.glob() function only searches for files in the specified directory and does not search in any of its subdirectories.
To search for files with a particular extension in the specified directory and all its subdirectories, you can use the os.listdir() function in a recursive manner.
Here’s an example implementation:
import os def find_files(dir_path, extension): # Check if the given path is a valid directory if not os.path.isdir(dir_path): return # Get a list of all the files and directories in the given directory files_and_dirs = os.listdir(dir_path) # Iterate over the list of files and directories for file_or_dir in files_and_dirs: # Construct the full path of the file or directory full_path = os.path.join(dir_path, file_or_dir) # If the full path is a directory, recursively call the function to find the files in that directory if os.path.isdir(full_path): find_files(full_path, extension) else: # If the full path is a file that ends with extension, print its path if full_path.endswith(extension): print(full_path)
This code defines a recursive function called find_files() that takes a directory path as its argument and searches for files in that directory and all its subdirectories.
It uses the os.listdir() function to get the names of all files and directories in the specified directory, and then uses the os.path.isdir() function to check if a given file or directory is a directory.
- If it is a directory, the function calls itself recursively to search for files with a specific extension in that directory.
- If it is a file, the function checks the extension and prints the file name out if it ends with the target extension.
Let’s call this function to see it in action.
Example
Let’s continue with the Example project folder with the following structure:
. ├── Data │ └── ages.txt │ └── numbers.txt ├── Images │ └── image.png ├── Samples │ └── codes.txt │ └── readme.txt ├── finder.py ├── names.txt └── values.txt
Our goal is to find all the .txt files in the entire folder (including the subfolders) by running a script in the finder.py file.
To do this, let’s copy the find_files function from the previous section to the finder.py file.
Let’s call the function by setting the current directory and the .txt as parameters.
./Data/numbers.txt ./Data/ages.txt ./Samples/readme.txt ./Samples/codes.txt ./names.txt ./values.txt
Awesome! This time the function not only found the text files at the same level as the python file but also the text files in the subfolders.
Even though this example offers great practice with recursion in Python, there’s an easier alternative for finding the files.
3. The ‘os.walk’ Function
The easiest way to recursively find all the files with a particular extension in a folder and its subfolders is by using the os.walk function.
Here’s what calling it might look like
import os for root, dirs, files in os.walk('/path/to/directory'): for file in files: if file.endswith('.someextension'): print(os.path.join(root, file))
This code imports the os module, which provides functions for interacting with the operating system. It then uses the os.walk() function to recursively iterate through all the directories and subdirectories within the specified directory (‘ /path/to/director y’).
For each directory in the tree, it sets three variables: root, dirs, and files. The root variable contains the path to the current directory, dirs is a list of subdirectories within that directory, and files is a list of files in the current directory.
It then loops through the list of files in the current directory and checks if each file ends with the specified extension. If it does, it prints the full path to that file using the os.path.join() function. This allows it to find all the files with the specified extension within the entire directory tree, not just the current directory.
This is a convenient replacement for the rather lengthy recursion approach from the earlier section.
Example
Let’s continue with the project structure you’ve already seen in this guide.
. ├── Data │ └── ages.txt │ └── numbers.txt ├── Images │ └── image.png ├── Samples │ └── codes.txt │ └── readme.txt ├── finder.py ├── names.txt └── values.txt
In particular, let’s find all the .txt files from the entire folder and its subfolders.
To do this, let’s modify the previously introduced code to target the current folder and to only show files with .txt extension:
import os for root, dirs, files in os.walk('.'): for file in files: if file.endswith('.txt'): print(os.path.join(root, file))
Running finder.py with the above code shows all the files with .txt extension:
Summary
Today you learned how to find files with a specific extension in Python.
To take home, use the os.walk() function to recursively walk through folders and subfolders to find all the files with a particular extension.
Thanks for reading. Happy coding!
Read Also
How To Find files with Certain Extension using Python
We know how to find files and do operations on that file using the file handling methods. But when we need to do certain operations on a file with a specific file extension like .txt , .PNG , .JPG , or .py , we need to find that file.
As we all know there are several methods to find the files, but in this tutorial, we will learn to find the files with certain extensions using the endswith() function, comprehension method, and the several methods present in the os module and glob module.
The endswith() is an in-built function that returns True in this case, if the string ends with a particular specified suffix; else it will return F alse . The glob.glob() function returns the file name with a specified pattern.
Example: Finding files in a directory using the listdir() Function
The below example shows how to find files in the directory using the listdir() function.
# Lists all files #import os module import os # Specifies the path in path variable path="C:\my_dir" for x in os.listdir(path): print(x)
Once we run the program we will get the following output.
instance_var_examples.py
mp4_1.mp4
mp4_5.mp4
practice.py
practice1.py
write opeartion.png
Example: Finding file using the endswith() Function
The below example shows how to find files in the directory with certain extension using the listdir() function and the endswith() function.
# Finding files with extension using for loop #import os module import os # Specifies the path in path variable path="C:\my_dir" for i in os.listdir(path): # List files with .py if i.endswith(".py"): print("Files with extension .py are:",i)
Once we run the program we will get the following output.
Files with extension .py are: instance_var_examples.py
Files with extension .py are: practice.py
Files with extension .py are: practice1.py
Files with extension .py are: Static_var.py
Files with extension .py are: variables_2.py
Files with extension .py are: var_1.py
Example: Finding files with a certain extension using the comprehension
The below example shows how to find files in the directory with a certain extension using the listdir() function and the endswith() function with the comprehension method.
# Using comprehension method import os path = 'C:\my_dir' files = [x for x in os.listdir(path) if x.endswith('.py')] print(files)
Once we run the program we will get the following output.
[‘instance_var_examples.py’, ‘practice.py’, ‘practice1.py’, ‘Static_var.py’, ‘variables_2.py’, ‘var_1.py’]Example: Finding files with a certain extension using the glob and os Module
The below example shows how to find files in the directory with a certain extension using the glob and os module.
# Another method using glob and os module #import glob and os module import glob import os os.chdir("C:\my_dir") for file in glob.glob("*.py"): print(file)
Once we run the program we will get the following output.
instance_var_examples.py
practice.py
practice1.py
Static_var.py
variables_2.py
var_1.py
Conclusion
In this tutorial, we learned how to find the files with specified extensions using the python in-built function endswith() and the glob and os module.