Open In App

Find all the Files in a Directory with .txt Extension in Python

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Directory traversal is a common operation performed by file locator in Operating Systems. The operating system offers elaborate methods for streamlining the search process and the choice to search for a selected filename/extension. This article will teach you how to find all the files in a directory with a .txt (text files) extension using Python. Several functions could perform the task efficiently. This article will go in-depth over the following methods:

  • Using the listdir function to obtain .txt files
  • Using the glob function to obtain .txt files
  • Using the walk function to obtain .txt files

For demonstration purposes, the following directory would be used:

Files in a Directory with .txt Extension in Python

 

Find all the Files in a Directory with .txt using listdir function

The listdir function, found inside the os library, is used to obtain all the files found within the directory specified by the argument to the function. The function returns a list containing all the files within the specified directory. The syntax for the function is:

listdir(path=None)

Example 

Firstly the path to the directory is specified in a variable. Then the variable is passed to the listdir function as an argument. The function returns a list of all filenames within the specified directory. A loop is run over each element of this list. In each loop iteration, a list’s element (containing filename) is checked to determine whether it ends with a .txt extension, using the endswith function. If it does, then the path to the directory is prepended to the filename, and the result is displayed. The process continues until all the list elements are exhausted. 

Python3




import os
 
# Path to the directory (absolute or relative)
dir = "C:\Users\Sauleyayan\Desktop\New folder"
 
# os.listdir return a list of all files within
# the specified directory
for file in os.listdir(dir):
 
        # The following condition checks whether
    # the filename ends with .txt or not
    if file.endswith(".txt"):
 
        # Appending the filename to the path to obtain
        # the fullpath of the file
        print(os.path.join(dir, file))


Output:

C:\Users\Sauleyayan\Desktop\New folder\bakup.txt
C:\Users\Sauleyayan\Desktop\New folder\buy.txt

Find all the Files in a Directory with .txt using the glob function

The glob library is a versatile library when it comes to filesystem processing. The library offers many methods which allow wildcard matching against the paths, which helps create minimal codes. For accomplishing the task at hand, the use of the glob function present inside the glob library would be made. The syntax of the function is as follows:

glob(pathname, *, recursive=False)
Returns a list of paths matching a pathname pattern.

Example 

The function takes into an argument a pathname (absolute or relative) along with optional wildcards. Hence, If the wildcards are not provided, then the function works simply as a directory traverser and returns a list of all filenames in the given directory. If wildcards are provided, only the filenames matching the given wildcard pattern will make it to the list. The following code utilizes the function to devise an answer to the problem.

Python3




import glob
 
# Path to the directory
dir = "_Path_to_dir_"
 
# Adding the wildcard of *.txt on the directory
dir_wild = dir + "\\" + "*.txt"
# for ubantu dir_wild = dir + '/' + '*.txt'
 
# The glob function would return a list of all
# filenames ending with .txt in the directory
# The loop iterates over all the those results
for file in glob.glob(dir_wild):
 
    # Displaying the absolute path to the file
    print(file)


Output:

C:\Users\Sauleyayan\Desktop\New folder\bakup.txt
C:\Users\Sauleyayan\Desktop\New folder\buy.txt

Find all the Files in a Directory with .txt using the walk function

A walk function present inside the os library generates the file names in a directory tree by walking the tree either top-down or bottom-up. Each directory in the tree rooted at the top (including the top itself) yields a 3-tuple (root: Prints out directories only from what you specified, dirs: Prints out sub-directories from the root, and files: Prints out all files from root and directories).

Example 

Firstly the path to the directory is defined similarly to the previous examples. Then in a loop, the walk function is called, and the variable storing the directory path is passed as an argument. As explained earlier, the function returns 3 values, namely the root, directories, and the files found in the given path. Where the directories and files are lists, and the root is a string. Inside the loop, all the filenames present inside the files list are iterated over in for a loop. In each iteration, the filename is checked to determine whether it ends with the .txt extension. If it does, then the full path to the file is displayed. Otherwise, the file is ignored. 

Python3




import os
 
# Path to the directory
dir = "_Path_to_dir_"
 
# The function walk returns a three tuple
# which is stored in separate variables
for (root, dirs, files) in os.walk(dir,
                                   topdown=True):
 
        # Iterating over the list of files
    for x in files:
 
        # Condition to check whether the file
        # ends with .txt extension or not
        if x.endswith(".txt"):
 
            # Appending the filename to the path to
            # obtain the fullpath of the file
            print(root + "\\" + x)


Output:

C:\Users\Sauleyayan\Desktop\New folder\bakup.txt
C:\Users\Sauleyayan\Desktop\New folder\buy.txt

Related Articles:

How to read multiple text files from a folder in Python

List all files of a certain type in a directory using Python



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads