How To Get a List of All Sub-Directories in the Current Directory Using Python?

Sometimes, it's very important to get the list of the files and the folders in a directory. The Pathlib module is used to get the list of all subdirectories in the current directory with Python's help. To get the list of all sub-directories in the current directory, the object of the path is created, which depends on the operating system. In Windows, the path of the object is provided by the Windows path; on the other hand, in Linux and macOS, it will return PosixPath.

Let's see a sample code:

Code:

Output:

C:\Users\Mark Johns\OneDrive\Desktop\javatpoint

Explanation:

In the above code, the pathlib module is imported, the path of the directory is passed to the Path function provided by the pathlib modules and the directory is printed.

Getting a List of All Files and Folders in a Directory in Python:

A generator is returned by the .iterdir() method when it is called on the Path object that shows the child items. If the generator is wrapped by the list() constructor, then the list of files and folders can be seen.

Code:

Output:

[WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/.vscode'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/documents'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/January'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/March'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/November'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/October'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/september')]

Explanation:

In the above code, the pathlib module is imported, and the path of the directory is passed to the Path object in the pathlib modules. With the help of the interdict () function, a generator is returned that consists of all the sub directories in the directory. With the help of a list object, all the subdirectories are printed.

With the help of a for loop, the subdirectory can also be printed.

Code:

Output:

C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\.vscode - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\august - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\december - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\documents - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\February - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\January - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\March - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\November - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\October - dir
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\september - dir

Explanation:

In the above code, with the help of a for loop, the type of the directory is also printed. Inside the for loop body, the f-string is used to display the information about each sub-directory. In the second curly braces in the f-string, the conditional expression is used to print dir if the item is a directory or file if it isn't.

Code:

Output:

C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\Algorithm to Solve Sudoku.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\Convert a datetime to a UTC timestamp in Python.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\Daily and Weekly Report April.xlsx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\email_spam.csv      
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\How to access the serial RS232 port in Python.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\How to call a Variable from Another Function in Python.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\How to convert a string to a Python class.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\How to Convert Excel to CSV in.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\How to create a .pyc file in Python.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\How to find mime type of file in Python.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\How To Get A List Of All Sub.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\main.py
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\module.py
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\Python requests.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\Split Pandas DataFrame by Rows in Python.docx
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\Untitled.ipynb      
C:\Users\Mark Johns\OneDrive\Desktop\javatpoint\April\~$w To Get A List Of All Sub.docx

Explanation:

In the above code, inside the for loop, if the directory is file type, then the item is printed.

Code:

Output:

[WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/.vscode'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/documents'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/January'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/March'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/November'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/October'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/september')]

Explanation:

In the above code, the list is filtered with the help of conditional expression within the comprehension to check if the item is a directory. But if all the files and directories in the subdirectories are needed, too. Then .iterdir() can be used as recursive function, but if is better to use r.glob() function to get the list of all subdirectories in the directory.

Recursively Listing With .rglob()

Sometimes, directories are compared with tress because of the recursive nature of the directory. Each main branch is split into sub-branches, and every sub-branch is off itself, too, and so on. If the items of the directory are listed recursively, that means the directory's content is not listed, but the content of the subdirectories, their subdirectories, are listed as well. With the help of the pathlib module, it's very easy to list the directory recursively. Using the .rglob() function, you can also list the directory.

Code:

Output:

[WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/.vscode'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/January'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/March'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/November'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/October'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/september'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/.vscode/settings.json'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/.ipynb_checkpoints'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/Algorithm to Solve Sudoku.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/Convert a datetime to a UTC timestamp in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/Daily and Weekly Report April.xlsx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/email_spam.csv'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/How to access the serial RS232 port in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/How to call a Variable from Another Function in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/How to convert a string to a Python class.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/How to Convert Excel to CSV in.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/How to create a .pyc file in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/How to find mime type of file in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/How To Get A List Of All Sub.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/main.py'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/module.py'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/Python requests.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/Split Pandas DataFrame by Rows in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/Untitled.ipynb'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/__pycache__'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/~$w To Get A List Of All Sub.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/.ipynb_checkpoints/Untitled-checkpoint.ipynb'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/April/__pycache__/module.cpython-39.pyc'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/Binomial heap program in C.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/binomial heap.c'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/C program to check student.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/Daily Report & Monthly Report format (2) (1).xlsx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/datatype.cpp'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/File Input and output operation in C.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/Fundamental Data type in C++.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/generic keyword'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/How to disable or enable the touchpad on a laptop.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/How to fix Blue Screen Error in windows.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/Introduction to Machine Learning and Its types.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/Introduction_to_Machine_Learning_and_Its_types[1].docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/main.c'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/ml.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/Python Data Type and Variables.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/Race Condition in OS.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/Role of Machine Learning in Data Science.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/Types of Computer Cables and Ports.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/august/What is IF ELSE.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Command Line Scripts with Python Packaging.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Converting string.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Copy all files from one directory to another using Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Correcting EOF error in python in Codechef.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Create a stacked bar plot using Matplotlib in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Create a watchdog in Python to look for filesystem changes.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Create Basic Webpage with Pyscript in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Create Classes Dynamically in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Create multiple copies of a string in Python by using multiplication.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Daily and Monthly Report December.xlsx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/How to install a Python Package from a GitHub Repository.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/How to Install NumPy on Windows.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/How to install setup tools for python on linux.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Is python backend or Frontend.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/Isoformat in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/numpy interpolation.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/pyscript'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/pyscript/hello_pyscript.html'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/pyscript/index.html'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/pyscript/main.py'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/pyscript/module.html'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/december/pyscript/my_module.py'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/.idea'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Convert Speech to text and text to Speech in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Convert String to Unicode Characters in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/covid-liver.csv'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Daily Monthly Report February.xlsx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Difference Between Lock and RLock objects in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Different Ways to Kill a Thread in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/file.txt'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Find DataType of Columns using Pandas DataFrame dtypes Property in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Get unique values from a list in.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/input.txt'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/main.py'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/new_folder'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/os.read() method in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/os.removedirs method in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/os.scandir() method in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Printing Lists as Tabular Data in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Programming Paradigms in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/PUT method.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Python Directory.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Python Program for Method Of False Position.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/Python PySpark collect.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/urandom method in Python.docx'), WindowsPath('C:/Users/Mark Johns/OneDrive/Desktop/javatpoint/February/.idea/.gitignore')]

Explanation:

In the above code, the .rglob('*') function is used to produce a generator that can be wrapped in a list() constructor to materialize, and the list of the sub-directories is printed recursively.

With the help of Python Glob Patter for conditional listing:

Sometimes, all the files are not needed. When is the type of directory file to be listed, or are all the items with a specific pattern of character in their name? A method related to .rglob() is the .glob() method. These methods use glob patterns that show the collection of paths. Wildcard characters are used to match certain criteria. Consider an example: everything in the directory is matched by a single asterisk.

There are some global patterns and their matches.

  1. * is used to match every item in the directory.
  2. *.txt is used to match every item in the directory that ends in .txt, such as notes.txt or hello.txt.
  3. ????? is used to match whose name is six characters long, such as 01.txt, A-01.c, or .zshrc
  4. A* is used to match each item with the start character as A, such as Album, A.txt, or AppData.
  5. [abc][abc][abc] is used to match each item whose name is three characters long but only composed of characters a, b, and c, such as abc, aaa, or cba.

Conclusion:

Getting list of all sub directories in a directory is very important and here are the some methods and ways to get the list of all sub directory in the directory.