Python list of files in a directory

“Python list of files in a directory,” as the term itself explains, to get a list of all files from a directory in python. Table of Contents What is a directory?When do we use the …

Python list of files in a directory

“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.

Import os 

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:

  • Os.listdir()
  • glob.glob ()

Os.listdir()

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.

Example1:

import os
print(os.listdir())
Output:
Python list of files in a directory

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.

Example2:

import os
print(os.listdir('./OneDrive'))

Output:

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.

Example3:

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)

Output:

Python list of files in a directory

As you can see in the output, we are only getting the list of files present in the “OneDrive.”

Glob.glob():

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.

Example4:

import glob
print(glob.glob("*"))

Output:

Python list of files in a directory

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():

Example5:

import glob
print(glob.glob("Downloads/*.pdf"))

Output:

Python list of files in a directory

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.

Conclusion:

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

Leave a Comment