How to use OS with Python List Comprehension Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 ComprehensionBelow are some of the example for using os with List Comprehension in Python: Listing Files in a DirectoryFiltering Files with a Specific ExtensionGetting SubdirectoriesFile Structure file structureListing Files in a DirectoryIn 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.pyone.txtthree.txttwo.txtFiltering Files with a Specific ExtensionIn 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.txtthree.txttwo.txtGetting SubdirectoriesIn 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:gfg1gfg2 Comment More infoAdvertise with us Next Article How to use OS with Python List Comprehension V vishuvaishnav3001 Follow Improve Article Tags : Python Python Programs Python list-programs Python os-module-programs Practice Tags : python 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 Split String into List of characters in Python We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g'].Using ListThe simplest way t 2 min read Convert set into a list in Python In Python, sets are unordered collections of unique elements. While they're great for membership tests and eliminating duplicates, sometimes you may need to convert a set into a list to perform operations like indexing, slicing, or sorting. For example, if input set is {1, 2, 3, 4} then Output shoul 3 min read Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the 3 min read How to Append Objects in a List in Python The simplest way to append an object in a list using append() method. This method adds an object to the end of the list. Pythona = [1, 2, 3] #Using append method to add object at the end a.append(4) print(a) Output[1, 2, 3, 4] Let's look into various other methods to append object in any list are:Ta 2 min read Like