“Python list of files in a directory,” as the term itself explains, to get a list of all files from a directory in python.
What is a directory?
It is a cluster of files that store pieces of information; basically, a directory is created for organizational purposes. To organize or arrange the files that contain the same type of data.
Well, I guess that much is enough. Let’s begin our main topic, “python list of files in a directory.”
When do we use the python list of files in a directory?
When you are working with the files in python, and you want to list all the python files, you will make use of the python list of files in a directory; for this, the python OS library provides some methods.
How can we acquire a python list of files in a directory?
Python contains its own python OS library, which provides us several approaches that we can acquire to list files in the directory.
The main python OS library approaches to get a list of files in a directory are:
Os.listdir() : this method will fetch the list of files and folders from a directory.
Glob.glob() : this method will bring the files in a directory based on the same pattern.
Os.walk() : this method will fetch all the folders and files present in the directory and within the sub-directories.
Os.scandir() : this method will bring the list of the items in the directory.
Pathlib : it is a library that you can use to list all the files present in a directory.
The python OS library is an external library to import it in code, and we will use the import function to fetch the os library into our code.
Once an OS library is imported, you can use its methods or functions to list files and folders in the directory.
Here we will discuss the most popular python Os library methods:
- glob.glob ()
Os.listdir() is a method that will fetch the list of files and folders from a given directory.
It only fetches the list of files and folders present in a directory; it does not list folders and files from sub-directories.
Let’s check an example.
import os print(os.listdir())
In this example, we have not given any specific path to load a list of files in a directory, or we have not passed any parameter in the statement.
So, this is fetching the list of all files from a current working directory, the “C” drive, where the python is placed.
For more, let’s take another example.
import os print(os.listdir('./OneDrive'))
In the above example2, we have specified the path of a directory, and it is only showing us the list of files from that directory.
As you can check the above example2, we used “.” This means we are referring to the directory from a relative path.
To fetch a list in a directory with the relative path, we use “.” or “..”
You can also fetch the list of files and folders in a directory from the absolute path.
(Relative path is a location that is relative to the current directory, whereas Absolute path is the exact location of your directory.)
Let’s see this with the help of an example.
import os from os.path import isfile, join # set the base path base_path = './OneDrive' file_ls = [f for f in os.listdir(base_path) if isfile(join(base_path, f))] print(file_ls)
As you can see in the output, we are only getting the list of files present in the “OneDrive.”
The glob method is also used to fetch the files and sub-directories present in a directory that’s based on the same pattern.
We can use the glob method as a glob.glob()
Let’s take an example to see how this works.
import glob print(glob.glob("*"))
In this example, we used “*” as a directory path, or you can say we have passed “*” as a parameter in the glob statement.
This will fetch all the files and folders from the current working directory.
Let’s take another example of glob.glob():
import glob print(glob.glob("Downloads/*.pdf"))
In the above example, we specified the name of the directory/folder whom we want to fetch the list of files and sub-directories, and in output, as you can see, we are only getting those.
Similarly, you can use other methods/functions of the python OS Library to fetch the list of files in a directory.
In this article, we discussed how to get a python list of files in a directory.
Which methods python OS library is providing us, and briefly explained the most common methods of python OS library Os.listdir() and glob.glob() .
Suggested Read: Remove Brackets from list Python