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.
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.
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.
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.
from os import listdir
from os.path import isfile, join
directory = "Airbus Event"
filesInCurrentDirectory = [f for f in listdir(directory) if isfile(join(directory, f))]
Alternatively, the same thing can be done using os.walk() as well.
from os import walk
listOfAllFiles = 
directory = "Airbus Event"
for (dirpath, dirnames, filenames) in walk(directory):
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.
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.
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!