Listing Directory Contents With Python

Christopher Franklin
Weekly Python
Published in
3 min readJan 5, 2021

As part of the Weekly Python Coding Challenge this week, you need to use Python to list the contents of a directory. This article will cover the three primary methods of accomplishing that using Python.

Using the “os” Module

The first two methods we will examine come from the os module.

Our first option is to use os.listdir(path='.') to grab the contents of the directory. This function takes in a path string and returns a list of all files and folders in that directory as strings. If you don’t pass in the path parameter, it will run on the current directory.

import osout = os.listdir()
print(out) # will return ['file.py', 'sub_folder', 'demo.txt', ...]

listdir has a few interesting behaviors that you should be careful about. First, the list of strings returned will be in an arbitrary order, and may not be the same between different uses. It also doesn’t include the special entries '.' and .. even if they exist.

A second behavior you need to careful with is if the path is non-existent. In this case, the function throws a FileNotFoundError . This is the same behavior that the Linux ls command has, but you need to keep it in mind as you use this function.

The final issue, especially if you are trying to do the Weekly Python Coding Challenge, is that you will need to make a second call to os.stat to get additional information about the file. For instance, if you want to know the file owner, you get that by calling os.stat on each file in the return.

If you plan on doing one-off lookups for files, os.listdir is a great way to accomplish this. However, if you want to work with every file in the directory it is recommended to use one of the other two functions.

The second method of interacting with directories is using os.scandir(path='.') . This function exposes a much more Object-Oriented way of interacting with the underlying file system. Instead of returning a list of path strings, it returns an Iterator pointing to a list of os.DirEntry objects.

import osout = os.scandir()
# out here is a <posix.ScandirIterator object at 0x1234567890>
for entry in out:
print(entry) # <DirEntry 'example.py'>

A DirEntry object exposes the path but it also includes additional file details while minimizing the total number of system calls made. This allows us to gather more information in a more performant manner.

As you might guess, system calls are relatively expensive to make. Because of this, the DirEntry class will cache the last result fetched. Aside from this feature, the two methods are very similar.

Using the “pathlib” module

Finally, we have the last method of listing a directory using the pathlib module. If you prefer to work with the underlying system in a way that resembled BASH, then you should use one of the first two methods. However, if you want to work with the underlying system in a very Pythonic Object-Oriented manner, the pathlib module is for you!

When working with the system, we use the pathlib.Path object to represent a system directory. We can then call Path.iterdir() and get back an object that is very similar to the os.DirEntry class we talk about above.

from pathlib import Pathdir_path = Path('./')
for entry in dir_path.iterdir(): # each entry is a Path object
print(entry) # prints the path string

This method is like a combination of the two methods from the os module. It is much more Object-Oriented, similar to the os.scandir method. However, it will return the results in an arbitrary order and with . and .. missing similar to os.listdir .

All three methods can be used to fetch the same data. The only difference is one of personal preference. Do you prefer to work with an Object-Oriented approach? Then using Path from pathlib is the method for you. If you want to work similar to a BASH script, use os.listdir instead.

If you are approaching the Weekly Python Coding Challenge, I strongly suggest you use os.scandir to improve performance and make your life easier.

Do you want to be notified when the next weekly challenge is released? Join the email notification list over here!

--

--