List All Files Of A Directory In Python

Python List All Files And Sub Directories Of A Folder

This is a quick tutorial on how you can list all files of a directory in Python. Here we’ve listed out several different methods to achieve this using python language.

Listing All Files & Sub-Directories In Python Using OS Module

You can use the os module in python to list all the files as well as subdirectories of a folder. All you need to do is to import the os module and then use the os.listdir() method on it. This method will take the directory path as a string. The demonstration of the code for the same is given below.

import os

I have a folder named Airbus Event on my desktop, so I navigated to the desktop using the cd command and then used the above two lines of code to list all the contents of this folder. Basically, the output is a list of all the files and sub-directories names.

Python List All Files And Sub Directories Using OS

You can also use os.listdir() without providing any directory path parameter as a string. In that case, it will automatically consider the current directory. The demonstration of the same is also given in the following screenshot. Here as I am on the desktop, so running this command listed all of the desktop files and folders.

OS Listdir Command

If you don’t want to get the sub-directories to be included in the result, then you can filter out the results using os.path.


List Only Files Inside Directory Using Python And OS Module

Alternatively, the same thing can be done using os.walk() as well.


List Of All Files Using OS.Walk()

Listing all files in Python Using Glob Module

There is also a glob module that can be used for the same purpose of listing all files present inside a directory using Python Language. Unlike the os module, the glob module is used for finding only files that are matching with a pattern. For example, if I only want to list all the files having .png extension, then I will execute the following python code.

import glob
print(glob.glob("Airbus Event/*.png"))

The following screenshot of the output for the above code shows the list containing all the files inside the queried folder but having the extension as PNG.

Python List Files Using Glob Matching Pattern

Similarly, you can use the glob module to generate a list of all the required files whose name matches a pattern. This is one of the widely used things in python and gots lots of beneficial use-cases.

There are numerous other ways to list out all the files and sub-directories of a current directory in Python. Here in this article, I’ve listed out the two most useful ways which can help you in solving most of your programming problems associated with files and folders listings.

You can also experiment with these functions and commands to further get more rich information like the full absolute path of each file and much more. If you know anything more useful associated with this article, let our visitors also know in the comments section.

I hope this article has helped you to list all the contents of a directory or a folder in Python. Do share it with others who might find this useful as well. Feel free to ask any kind of questions related to this article in the comments section.

Don’t forget to Subscribe To WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *