How to List All Files of a Directory in Python?

Have you ever tried to print out all the files in a directory, only to realize that there’s no easy way to do it? If you’re familiar with Python, then you know that one of its greatest strength is the ease with which it allows you to manipulate data. In this blog post, we’ll show you how to use Python to list all files of a directory—and how to add them to a list!

Listing all files of a directory in Python

There are a few different ways to go about listing all files of a directory in Python. The most common method is to use the os library. The os library has a number of functions that allow us to manipulate files and directories (or “folders”). In particular, the os.listdir function allows us to easily list all files of a directory.

To use the os library, we first need to import it. We can do this by adding the following line at the beginning of our Python script:

import os
Code language: Python (python)

Once we’ve imported the os library, we can use the os.listdir function by passing it the path of the directory whose contents we want to list as follows:

files = os.listdir("D:\mydir")
Code language: Python (python)

The above code will store a list of all files in the specified directory in the variable “files”. We can then print out this list using a for loop:

for file in files: print(file)
Code language: Python (python)
list files of a folder


abc.rtf mypython.txt subdir
Code language: CSS (css)

Adding Files to Lists

Now that we know how to list all files in a directory, let’s take things one step further and learn how add them to a list! This can be useful if we want perform some sort of operation on all files in a directory (e.g., renaming them or deleting them).

Luckily, adding items to lists is just as easy as listing them. All we need to do is use the append method. This method takes an item as input and adds it to the end of an existing list. For example, if we have an existing list called “my_list” and we want add an item called “new_item” to it, we would write:

Code language: JavaScript (javascript)

Let’s see how this works in practice by modifying our previous code example so that it adds the files in our directory not just prints them out:

my_list = [] #create an empty list files = os.listdir("D:\mydir") #store a list of all files in specified directory #add each file from directory into our empty list for file in files: my_list.append(file) #print out entire contents of our list for file in my_list: print(file)
Code language: Python (python)


['abc.rtf', 'mypython.txt', 'subdir']
Code language: JSON / JSON with Comments (json)
add to list

As you can see, once we’ve added the append line, our code will now store all files from our specified directory into a new list called my_list which we can then manipulate however we please!

Others to list all files of the given directory

Python script 1: To get only .txt files.

import os for y in os.listdir("."): if y.endswith(".txt"): print(y) # Prints only text file present in My Folder
Code language: Python (python)

Python script 2: listing all files: *.txt in the Folder: “D:\mydir\”

import glob path = "D:\mydir\*.txt" for file in glob.iglob(path, recursive=True): print(file)
Code language: Python (python)

Python script 3: With the glob() method, we can use “wild cards” (“*,?, [ranges]) to make getting a path easier and more convenient.

import glob # This is my path mypath = "D:\mydir" # I Use '*' pattern for files in glob.glob(mypath+ '*'): print(files) # I Use '?' pattern for files in glob.glob(mypath + '?.txt'): print(files) # I Use [0-9] pattern for files in glob.glob(mypath + '/*[0-9].*'): print(files)
Code language: Python (python)

Python script 4: List only files from a directory

import os # folder path mypath = "D:\mydir" # list to store files result = [] # Iterate directory for path in os.listdir(mypath): # check if current path is a file if os.path.isfile(os.path.join(mypath, path)): result.append(path) print(result)
Code language: Python (python)

Python script 5: If you know how to use a generator expression, you can use a generator function to make code smaller and easier to understand, as shown below.

import os def get_files(path): for file in os.listdir(path): if os.path.isfile(os.path.join(path, file)): yield file # Then simply call it whenever required. for file in get_files("D:\mydir"): print(file)
Code language: Python (python)

Python script 6: Using the os.walk() function we can list all directories, subdirectories, and files in a given directory.

from os import walk # folder path my_path = "D:\mydir" # list to store files name result = [] for (my_path, dir_names, file_names) in walk(my_path): result.extend(file_names) print(result)
Code language: Python (python)

Python script 7: os.scandir() to get files of a directory

import os # get all files inside a specific folder my_path = "D:\mydir" for path in os.scandir(my_path): if path.is_file(): print(
Code language: Python (python)

Python script 8: Pathlib Module to list files of a directory

import pathlib # folder path my_path = "D:\mydir" # to store file names result = [] # construct path object d = pathlib.Path(my_path) # iterate directory for file in d.iterdir(): # check if it a file if file.is_file(): result.append(file) print(result)
Code language: PHP (php)

Python script 9: Make list files function more efficient

If there aren’t many files, the list files() function works well. But when there are a lot of files, it is not good for memory to return a long list of files.

To fix this, you can use a generator to return each file individually instead of a list:

import os def list_files(path, extentions=None): """ List all files in a directory specified by path Args: path - the root directory path extensions - a iterator of file extensions to include, pass None to get all files. Returns: A list of files specified by extensions """ for root, _, files in os.walk(path): for file in files: if extentions is None: yield os.path.join(root, file) else: for ext in extentions: if file.endswith(ext): yield os.path.join(root, file) if __name__ == '__main__': filepaths = list_files(r'D:\mydir', ('.txt', '.docx')) for filepath in filepaths: print(filepath)
Code language: Python (python)


That’s it! You now know how to list all files of a given directory and add them into lists using Python. Stay tuned for more blog posts that will explore different ways to increase your efficiency when working with lists. Thanks for reading!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts