Last week, we began our series on the Python OS module by describing how to set your working directory. This week, we’re going to extend this functionality by showing you how to list all files in a folder using the Python listdir and walk methods.

Recall that during our series on the Python OS module, we’ll be using the following directory structure:

raw image

Our last tutorial left off with our current working directory set to C:\main directory using the following script:

import os
os.chdir(r"C:\main directory")
wd = os.getcwd()

This will be our starting point for this week’s tutorial on listing files in folder.

Listing Files and Folders in a Directory

It’s worth mentioning that the words folder and directory are used interchangeably. Normally a top-level folder is called a directory, but you’ll see the use of both these words interchangeably in this tutorial.

To get a list of all files and folders in a directory, use the listdir() method. You need to pass the path to the directory as a parameter value to the listdir() method. The following script lists all the files and folders in your current working directory.

import os
wd  = os.getcwd()
print(os.listdir(wd ))

Output:

You can see that our example current working directory has two folders: documents, and pictures, and three files: my_textfile.txt, my_textfile2.txt, and my_wordfile.docx.

['documents', 'my_textfile.txt', 'my_textfile2.txt', 'my_wordfile.docx', 'pictures'

The listdir() method only lists all the top level files and folders inside a directory. If you want to navigate all the lower level files and folders inside your directory and subdirectories, use the walk() method from the OS module.

The walk() method returns an iterator which you can use to hierarchically navigate through all the sub-directories and files inside another directory.

The following script lists all the subdirectories and files inside your current working directory.

import os
wd  = os.getcwd()
directories = os.walk(wd)
[print(x) for x in directories]

The output shows all the top level sub-directories and files inside your current working directory. It even “walks” down your directory structure to show the files and folders inside subdirectories

[('C:\\main directory',
  ['documents', 'pictures'],
  ['my_textfile.txt', 'my_textfile2.txt', 'my_wordfile.docx']),
 ('C:\\main directory\\documents',
  ['personal documents', 'professional documents'],
  []),
 ('C:\\main directory\\documents\\personal documents',
  [],
  ['my cv.docx', 'research statement.docx']),
 ('C:\\main directory\\documents\\professional documents', [], []),
 ('C:\\main directory\\pictures', [], [])]

The walk() iterator actually returns a tuple that consists of the directory path, the subdirectories inside the main directory, and the files inside each directory. Because of the tuple’s structure, you can separately list directories and filenames if you want. This is important because it lets you list all files in a folder while ignoring folder names.

The following script, for example, returns the directory path of your current working directory, folders inside the directory, and files inside the directory.

import os
wd  = os.getcwd()
directories = os.walk(wd)
dirpath, dirnames, filenames = next(directories)
print("Directory Path:", dirpath)
print("Directory Names:", dirnames)
print("File Names:", filenames)

Output:

Directory Path: C:\main directory
Directory Names: ['documents', 'pictures']
File Names: ['my_textfile.txt', 'my_textfile2.txt', 'my_wordfile.docx']

If you iterate through the walk() iterator a second time using the next() method, you’ll see the directory path, folders, and files inside the C:\main directory\documents folder. Simply add a duplicate line like this to drill down multiple levels:

dirpath, dirnames, filenames = next(directories)
dirpath, dirnames, filenames = next(directories)

Our next tutorial will show how to open files using the Python OS module. Make sure you catch it by subscribing using the form below!


Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit