How to use OS with Python List Comprehension
Last Updated :
28 Apr, 2025
One such module is the 'os' module, which allows interaction with the operating system. When combined with list comprehensions, the 'os' module becomes a potent tool for handling file and directory operations concisely and expressively. In this article, we will see how we can use the os module with list comprehension in Python.
OS with Python List Comprehension
Below are some of the example for using os with List Comprehension in Python:
- Listing Files in a Directory
- Filtering Files with a Specific Extension
- Getting Subdirectories
File Structure
file structureListing Files in a Directory
In this example, in below code the Python OS module to list and display the names of files within the specified 'gfg' directory. The 'list_files' function employs list comprehension to filter only files (not directories) and then prints the resulting list.
Python3
# importing os module
import os
def list_files(directory_path):
# list comprehension
files = [file for file in os.listdir(directory_path) if os.path.isfile(
os.path.join(directory_path, file))]
# displaying the names available
print("List of files in the directory:")
for file in files:
print(file)
# main function
if __name__ == "__main__":
directory_path = "gfg"
list_files(directory_path)
Output:
List of files in the directory:
four.py
one.txt
three.txt
two.txt
Filtering Files with a Specific Extension
In this example, below code uses the 'os' module to filter and display the names of files with the '.txt' extension within the specified 'gfg' directory. The 'filter_file' function employs list comprehension to select only files with the target extension, and then it prints the resulting list.
Python3
# importing os module
import os
def filter_file(directory_path, target_extension):
# list comprehension
filtered_files = [file for file in os.listdir(directory_path) if file.endswith(
target_extension) and os.path.isfile(os.path.join(directory_path, file))]
# displaying the names available
print(f"List of {target_extension} files in the directory:")
for file in filtered_files:
print(file)
# main function
if __name__ == "__main__":
directory_path = "gfg"
target_extension = ".txt"
filter_file(directory_path, target_extension)
Output:
List of .txt files in the directory:
one.txt
three.txt
two.txt
Getting Subdirectories
In this example, below code uses the 'os' module to retrieve and display the names of subdirectories within the specified 'gfg' parent directory. The 'get_subdirectories' function employs list comprehension to filter only items that are directories, and then it prints the resulting list.
Python3
# importing os module
import os
def get_subdirectories(parent_directory):
# list comprehension
subdirectories = [subdir for subdir in os.listdir(
parent_directory) if os.path.isdir(os.path.join(parent_directory, subdir))]
# displaying the names available
print("List of subdirectories in the parent directory:")
for subdir in subdirectories:
print(subdir)
# main function
if __name__ == "__main__":
parent_directory = "gfg"
get_subdirectories(parent_directory)
Output:
List of subdirectories in the parent directory:
gfg1
gfg2
Similar Reads
Python List Comprehension With Two Lists List comprehension allows us to generate new lists based on existing ones. Sometimes, we need to work with two lists together, performing operations or combining elements from both. Let's explore a few methods to use list comprehension with two lists.Using zip() with List ComprehensionOne of the mos
3 min read
Two For Loops in List Comprehension - Python List comprehension is a concise and readable way to create lists in Python. Using two for loops in list comprehension is a great way to handle nested iterations and generate complex lists. Using two for-loopsThis is the simplest and most efficient way to write two for loops in a list comprehension.
2 min read
4 Tips To Master Python List Comprehensions Python's list comprehensions offer a concise and powerful way to create lists. They allow you to express complex operations in a single line of code, making your code more readable and efficient. In this article, we will explore four tips to master Python list comprehensions with five commonly used
4 min read
How Use Linux Command In Python Using System.Os Using the system module from the os library in Python allows you to interact with the Linux command line directly from your Python script. This module provides a way to execute system commands, enabling you to automate various tasks by integrating Linux commands seamlessly into your Python code. Whe
3 min read
Python - List of float to string conversion When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them
3 min read
How to Input a List in Python using For Loop Using a for loop to take list input is a simple and common method. It allows users to enter multiple values one by one, storing them in a list. This approach is flexible and works well when the number of inputs is known in advance.Letâs start with a basic way to input a list using a for loop in Pyth
2 min read