How to Find Files Using Python

Muhammad Maisam Abbas Feb 02, 2024
  1. Find File With the os.walk() Function in Python
  2. Find File With the glob.glob() Function in Python
  3. Find File With the Path.glob() Function in Python
How to Find Files Using Python

This tutorial will discuss the methods to find a file in Python.

Find File With the os.walk() Function in Python

If we want to find the path of a specific file on our machine with python, we can use the os module. The os module provides many os-related functionalities to our code. The os.walk() function takes a path string as an input parameter and gives us the directory path, the directory name, and the filename for each file in the path. The sample code below shows us how to find a file in Python with the os.walk() function.

import os


def findfile(name, path):
    for dirpath, dirname, filename in os.walk(path):
        if name in filename:
            return os.path.join(dirpath, name)


filepath = findfile("file2.txt", "/")
print(filepath)

Output:

/Users\maisa\Documents\PythonProjects\file2.txt

In the above code, we declared the findfile() function that uses os.walk() function to find our file. The findfile() function takes the file’s name and the root path as input parameters and returns the path of our specified file. This approach gives us the absolute path of the file.

Find File With the glob.glob() Function in Python

We can also use the glob.glob() function to solve our current problem. The glob.glob() function takes a pathname as an input parameter and returns a list of all the file paths that match the input argument. We can specify a regular expression as an input parameter that matches our file only. The sample code below shows us how to find a file in Python with the glob.glob() function.

import glob

filepath = glob.glob("**/file.txt", recursive=True)
print(filepath)

Output:

['Find File\\file.txt']

We passed our file name as the input parameter to the glob.glob() function, and it returned the relative path of our file. This method can give us the relative path as well as the absolute path of our file.

Find File With the Path.glob() Function in Python

Another approach is to use the pathlib module. This Python module offers classes that represent filesystem paths for different operating systems. We can use the Path.glob() function inside the pathlib module to solve our specific problem. This function is similar to the glob() function inside the glob module. The Path.glob() function takes a pattern as an input parameter and returns a list of path objects that match the input argument. The sample code snippet shows us how to find a file in Python with the pathlib module.

import pathlib

filepath = sorted(pathlib.Path(".").glob("**/file2.txt"))
print(filepath)

Output:

[WindowsPath('file2.txt')]

We passed a pattern string that matches our file to the Path.glob() function. The Path.glob() function returns us a list of WindowsPath objects that match the pattern. With this method, we get path objects specific to our operating system.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Python File