How to use `os` module In Python

This module provides a portable way of using operating system-dependent functionality. The method os.listdir() lists all the files present in a directory. We can make use of os.walk() if we want to work with sub-directories as well.

Syntax:

 os.listdir(path = ‘.’)

Returns a list containing the names of the entries in the directory given by path. 

Syntax:

 os.walk(top, topdown=True, onerror=None, followlinks=False)

Generates the file names in a directory tree by walking the tree either top-down or bottom-up.

Example 1: List the files and directories present in root/home/project

Python




import os
 
# To get directories as well as files present
# in a path
list_1 = os.listdir(path=r"root/home/project")
print(list_1)
 
# To get only files present in a path
list_2 = os.listdir(path=r"root/home/project")
 
# Loop through each value in the list_2
for val in list_2:
   
    # Remove the value from list_2 if the "." is
    # not present in value
    if "." not in val:
        list_2.remove(val)
print(list_2)


Example 1.5: List only the files, by using os.path.isfile function.

Python3




import os
 
print("Python Program to print list the files in a directory.")
 
Direc = input(r"Enter the path of the folder: ")
print(f"Files in the directory: {Direc}")
 
files = os.listdir(Direc)
files = [f for f in files if os.path.isfile(Direc+'/'+f)] #Filtering only the files.
print(*files, sep="\n")
 
 
 
#os.getcwd() gives us the current working directory, and os.listdir lists the director


Output :

['documents', 'code', 'charter.xlsx', 'timeline.jpg']
['charter.xlsx', 'timeline.jpg']

Example 2: List all the subdirectories and sub-files present in root/home/project

Python




import os
 
all_files = list()
all_dirs = list()
 
# Iterate for each dict object in os.walk()
for root, dirs, files in os.walk("root/home/project"):
    # Add the files list to the all_files list
    all_files.extend(files)
    # Add the dirs list to the all_dirs list
    all_dirs.extend(dirs)
 
print(all_files)
print(all_dirs)


Output:

[‘charter.xlsx’, ‘timeline.jpg’, ‘report.txt’, ‘workbook.pdf’, ‘trigger.sql’, ‘schema_template.py’, ‘sqlalchemy_models.py’, ‘info.log’, ‘README.md’, ‘requirements.txt’, ‘main.py’]

[‘documents’, ‘code’, ‘database_models’] 

Python – List files in directory with extension

In this article, we will discuss different use cases where we want to list the files with their extensions present in a directory using python.

Similar Reads

Modules Used

os: The OS module in Python provides functions for interacting with the operating system. glob: In Python, the glob module is used to retrieve files/pathnames matching a specified pattern. The pattern rules of glob follow standard Unix path expansion rules. It is also predicted that according to benchmarks it is faster than other methods to match pathnames in directories....

Directory Structure in use:

...

Method 1: Using `os` module

This module provides a portable way of using operating system-dependent functionality. The method os.listdir() lists all the files present in a directory. We can make use of os.walk() if we want to work with sub-directories as well....

Method 2: Using `glob` module

...