find .txt files in directory

How to Find All Text Files in Directory in Python

Often you may need to find all text files in directory, as part of your python script, application or website. In this article, we will learn how to find all text files in directory in Python. You can use it to not only search for .txt files but also files of other extensions such as .pdf, .csv, etc. This is useful for searching a particular kind of files and listing them on your website or application. In fact, you can even customize it to search for multiple file types at once.


How to Find All Text Files in Directory in Python

There are several libraries to find and list all text files in directory in Python.


1. Using glob

The glob module finds pathnames matching a given pattern, as per UNIX shell rules. We will use this library to get a list of all .txt files in a directory.

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
    print(file)

In the above code, we import glob and os modules. We use os.chdir() function to go to the folder where we need to look for .txt files, for example, /mydir. We call glob.glob() function to list all pathnames matching the pattern ‘*.txt’ for text files. It returns a list, which we loop through and display the file contents.

If you want to look for another different file type, such as .pdf files, replace *.txt above with *.pdf.


2. Using os.listdir()

os.listdir() function also lists all files and directories in a given directory.

import os
for file in os.listdir("/mydir"):
    if file.endswith(".txt"):
        print(os.path.join("/mydir", file))

In the above code, we run a for loop through the list of files and directories returned by os.listdir() function, called on our directory ‘/mydir’, where we look for .txt files. In each iteration of the loop, we call endswith() function to check if the file path’s extension is .txt or not. If it is .txt, we print the file’s path using os.path.join() function.


3. Using os.walk()

You can also use os.walk() to get a list of text files in a directory. The main difference between os.walk() and os.listdir() is that os.walk() returns only the file paths in specified directory’s tree while os.listdir() will list both files and directories. Secondly. when you use os.walk() you can specify the order of directory traversal, that is, start from top, bottom, etc.

Here is the code snippet to list all .txt files in directory /mydir.

import os
for root, dirs, files in os.walk("/mydir"):
    for file in files:
        if file.endswith(".txt"):
             print(os.path.join(root, file))

In the above code, we call os.walk() on /mydir directory, which returns root, directories and files. We loop through each of them and within each loop, we loop through the files in each subfolder. Here also, we call endswith() function to check the extension of each file. If it is .txt, then we print the file path.

In this article, we have learnt several ways to list all text files in directory using python. Generally, such code snippets are part of bigger scripts & applications. You can customize it as per your requirement by changing the target search directory as well as the file extension to be searched. You can even customize it to search for multiple file types by using multiple endswith() function calls combine with OR operator (file.endswith(‘.txt’) or file.endswith(‘.pdf’)).

Also read:

How to Convert CSV to Tab Delimited File in Python
How to Stress Test Linux Server
Schedule Cron Job Every 1 Hour
How to Send Message to Logged User in Linux
How to Delete User Accounts With Home Directory

Leave a Reply

Your email address will not be published.