12) Modules and Automation Lesson

Python List Files in Directory

6 min to complete · By Martin Breuss

In the previous lesson, you learned how you can get the path of your current working directory using Python's pathlib module. In this lesson, you'll build on top of this and use such a path to list what files are inside this directory.

How to List All Files

When you are working with a pathlib.Path object, such as the one you created in the previous lesson, you can do a couple of things with it out of the box.

For example, you can list all the files that are inside that folder using a for loop:

import pathlib

path = pathlib.Path.cwd()

for filepath in path.iterdir():
    print(filepath)

In this code snippet, you are using .iterdir() to create a collection of all the file paths of the files that are inside of the current working directory. Then, you iterate over each of them and print them out to your console.

Colorful illustration of a light bulb

Info: You've encountered a collection before when you learned about strings in Python. There are other data types that are collections in Python, and you'll get to know more of them as you keep moving along.

The output you receive is quite long! Those are all the absolute paths of all files inside this directory.

Display Only File Names

You might just be interested in the file names, however, and pathlib.Path objects have a way to show you just that:

import pathlib

path = pathlib.Path.cwd()

for filepath in path.iterdir():
    print(filepath.name)

Use the Name Parameter

The only change you applied to this code is that you added .name to the filepath variable. In fact, you can get the .name attribute of any pathlib.Path object, whether it's a folder or a file:

import pathlib

pathlib.Path.cwd().name  # python-course

In this snippet, you created a pathlib.Path object for your current working directory, then accessed the .name attribute. This will be the name of the folder you saved your script in.

Create a Specific Path Object

Now you have the chance to inspect your current working directory, but unless you saved your Python file right on your Desktop, it won't be where all the screenshots are. So how can you point Python to another location?

When you have a string that represents a path on your file system, such as the one you got from running .cwd(), you can pass it to a pathlib.Path object and start interacting with it.

Colorful illustration of a light bulb

Info: Python's pathlib handles both UNIX and Windows paths, which is one of the reasons that it's a great choice if you're working with your file system through Python.

Use the Path Parameter

This means that instead of executing .cwd(), you can also pass in a string that points to a certain location on your computer:

import pathlib

pathlib.Path("/Users/martin/Desktop")

The code snippet above creates an object that points to my Desktop on a UNIX system. If you passed in the equivalent path for your own Desktop, it'll point you there instead.

Practice Listing Files

  • Assign the path to your own Desktop to a variable, then play around with it.
  • Display only the name of the folder.
  • List all the files that are currently on your Desktop.
  • Check out the documentation for .suffix, .is_dir(), .parent, and .stem. Try them out and investigate what they each do.

You can see that you're already getting closer to cleaning up your Desktop. You can already see all the mess in there by running your Python script. So, your next step will be to filter out the screenshots and move them to a new folder.

You can feel how the power of pathlib is unfolding beneath your fingers.

Summary: Python List Files in Directory

  • List all files in a certain location using a loop and pathlib.Path.iterdir()
  • Display only the filenames of files with name parameter
  • Create a new pathlib.Path object by passing it a specific file path as a parameter