Get a list of all subdirectories in the current directory in Python
Among the methods Python provides for getting a list of the subdirectories within a given directory, there are a few methods that can be used in this tutorial. Here are three approaches that you can use:
Method 1: By Using the os.listdir() function
The first method is to use the os.listdir()
function, which returns a list of all the files and directories in a given directory. To get a list of only the subdirectories, you can use a list comprehension to filter out the non-directory items:
import os def get_subdirs_1(): # Get a list of all files and directories in the current directory all_items = os.listdir('.') # Use a list comprehension to filter out the non-directory items subdirs = [item for item in all_items if os.path.isdir(item)] return subdirs subdirs = get_subdirs_1() print(subdirs)
Program to get the list of all the subdirectories and the files in them:
We will get the list of files and subdirectories located in the current directory in Python using the OS module, this module commands our operating system to do what we require from our system. Here is an example:
# import the os module import os # list of subdirectories present in the directory all_dirs=os.listdir() print(all_dirs)
Output: ['03 string', '05 Variable.py', '06 operators.py', '08 input function.py', '09 practice set ch2.py', '10 string.py','bebu', 'codespeedy', 'conditional programming', 'CS50', 'dictionary and sets', 'File', 'file I and O', 'Functions and Recurssions', 'game', 'GCD_NPTEL', 'Inheritance and more on OOPS', 'Introduction to python', 'Lists and tuples', 'Loops', 'oops', 'pandas', 'play.mp3.mp3', 'Screenshots', 'Variables and datatypes']
Here are all the subdirectories and files present in the current directory.
Get the list of subdirectories with a ‘/’ at the starting:
In this example, you will be able to trace a list of only the subdirectories located in the current working directory in Python using the same OS module.
# import os module import os # path of the current directory path = 'path of the directory' # for loop to get a list of subdirectories for file in os.listdir(path): a = os.path.join(path, file) if os.path.isdir(a): print(a)
Output:
Get the list of subdirectories with no ‘/’ backslash
In example 3, we will be able to get a clearer list of all the subdirectories present in the current directory in Python.
# import os module import os # path of the current directory path = 'Path of the directory' # for loop to get a list of subdirectories for root, dirs,files in os.walk(path): for y in dirs: print(y)
Output:
Method 2: Using the os.scandir() function
A second method is to use the os.scandir()
function, which returns an iterator over the entries in a given directory. To get a list of the subdirectories, you can use a list comprehension to iterate over the entries and select only the directories:
import os def get_subdirs_2(): # Get an iterator over the entries in the current directory entries = os.scandir('.') # Use a list comprehension to select only the directories subdirs = [entry.name for entry in entries if entry.is_dir()] return subdirs subdirs = get_subdirs_2() print(subdirs)
Method 3: Using the glob module
A third method is to use the glob
module, which provides a function for generating file names matching a given pattern. To get a list of all the subdirectories in the current directory, you can use the glob.glob()
function with the pattern '*'
:
import glob def get_subdirs_3(): # Use the glob.glob() function to get a list of all subdirectories subdirs = glob.glob('*') return subdirs subdirs = get_subdirs_3() print(subdirs)
Conclusion
In this tutorial, we have used three different methods for getting a list of all the subdirectories in the current directory in Python. The os.listdir()
function and the os.scandir()
function can be used to get a list of all the items in a directory, and then filter out the non-directory items. The glob
module can be used to generate a list of file names matching a given pattern, which can be used to get a list of all the subdirectories.
Leave a Reply