Get a list of files in directory using python

List of all files in a directory using Python

It can be very handy to know how to programmatically get a list of all files in a folder. For example, you have a folder full of text files containing useful data that you want to collate into a dataset or you just want to find out whether a given file exists in a folder or not. In this tutorial, we will look at how to get a list of all the files in a folder using Python.

There are a number of ways to get a list of all files in a directory using Python. You can use the os module’s os.listdir() or the glob module’s glob.glob() functions to list out the contents of a directory.

Let’s demonstrate the usage for each of these methods with the help of some examples. First, let’s look at the directory structure of the directory we want to use for this tutorial.

Directory structure of the current working directory called weather.

The “weather” directory contains one python script, one requirements text file, one README markdown file, and a directory named “data” which stores the data for the project.

The os module in python comes with a number of handy functions for file handling. To list out the contents of a directory, you can use the os.listdir() function. It returns a list of all files and directories in a directory.

For example, let’s use it to get the list of contents in the current working directory which is the “weather” directory from the tree shown above.

import os
print(os.listdir())

Output:

['data', 'README.md', 'requirements.txt', 'train.py']

You can see we get all the files and directories in the current working directory. You can, however, pass a custom directory path to list out its contents instead. For example, let’s list out the contents of the “data” directory present inside the current working directory.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

import os
print(os.listdir('./data'))

Output:

['chennai.txt', 'data sources', 'delhi.txt', 'kolkata.txt', 'mumbai.txt', 'test_set.csv', 'train_set.csv']

We get a list of all files and folders present in the “data” directory. In this example, we passed a relative path but you can also pass an absolute path and get its contents as well.

If you only want to get a list of files and not the directories, you can use the os.path.isfile() function which checks whether a given path is a file or not. For example, let’s list out only the files (and not directories) inside the “data” directory.

import os
from os.path import isfile, join

# set the base path
base_path = './data'
file_ls = [f for f in os.listdir(base_path) if isfile(join(base_path, f))]
print(file_ls)

Output:

['chennai.txt', 'delhi.txt', 'kolkata.txt', 'mumbai.txt', 'test_set.csv', 'train_set.csv']

You can see that we only get the files and not the directories present inside the “data” folder.

For more on the os module in python, refer to its documentation.

You can also use the glob module to get a list of files in a directory. Let’s use it to list out the files in our current directory.

import glob
print(glob.glob("*"))

Output:

['data', 'README.md', 'requirements.txt', 'train.py']

You can see that we get all the files and directories in the current working directory. Note that we passed “*” as the parameter to the glob.glob() function which results in listing all the files and folders in the given directory.

You can also specify the types of files you want to get from a path. For example, to only get text files from the “data” folder in our current working directory –

import glob
print(glob.glob("data/*.txt"))

Output:

['data\\chennai.txt', 'data\\delhi.txt', 'data\\kolkata.txt', 'data\\mumbai.txt']

We get a list of only the text files present in the “data” folder. Note that the above result is obtained on a Windows machine hence the “\\” in the path.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Tutorials on interacting with the file system in Python –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top