How to Delete Only Empty Folders in Python Last Updated : 04 Dec, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to delete only empty folders in Python. Before deleting the folder, here is an image showing the files inside the main directory. As the above image is showing that the folder has 2 empty sub-folders and 1 non-empty folder. So, after running the code these 2 folders must get deleted. By using os module With the help of the module, we can easily get the files of any directory. Python3 import os for item in os.listdir(os.getcwd()): # print(item) # check dir if os.path.isdir(item): if os.listdir(item): print(os.path.join(os.getcwd(),item)) os.removedirs(os.path.join(os.getcwd(),item)) Output: By using loop In this method, we will use the loop to find the content inside each folder. Then if the folder is empty, we will delete it. Python3 import os root = 'C:\\Users\\Untitled Folder\\' folders = list(os.walk(root))[1:] for folder in folders: print("All Folder -> ",folder) if not folder[2]: os.rmdir(folder[0]) Output: All Folder -> ('C:\\Users\\Untitled Folder\\.ipynb_checkpoints', [], ['Untitled-checkpoint.ipynb']) All Folder -> (C:\\Users\\Untitled Folder\\Empty Folder 1', [], []) All Folder -> ('C:\\Users\\Untitled Folder\\Empty Folder 2', [], []) All Folder -> ('C:\\Users\\Untitled Folder\\Non empty folder', [], ['untitled.txt']) Comment More infoAdvertise with us Next Article How to Delete Only Empty Folders in Python K kumarsaurabhsingh05 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2022 Practice Tags : python Similar Reads How to delete a CSV file in Python? In this article, we are going to delete a CSV file in Python. CSV (Comma-separated values file) is the most commonly used file format to handle tabular data. The data values are separated by, (comma). The first line gives the names of the columns and after the next line the values of each column. Ap 2 min read How to delete data from file in Python When data is no longer needed, itâs important to free up space for more relevant information. Python's file handling capabilities allow us to manage files easily, whether it's deleting entire files, clearing contents or removing specific data.For more on file handling, check out:File Handling in Pyt 3 min read How to Delete files in Python using send2trash module? In this article, we will see how to safely delete files and folders using the send2trash module in Python. Using send2trash, we can send files to the Trash or Recycle Bin instead of permanently deleting them. The OS module's unlink(), remove() and rmdir() functions can be used to delete files or fol 2 min read Delete all the Png Images from a Folder in Python Python is mostly used to automate tasks, including file management operations. Deleting all PNG images from a folder can be efficiently handled using Python. In this article, we will explore two different approaches to Deleting all the PNG images from a Folder in Python. Delete all the PNG images fr 2 min read Delete files older than N days in Python In this article, we will learn to delete files that are older than a given number of days using Python. This work can be done manually but it is difficult to do when files are more in number, so we will use Python to make this task simple. We will learn three ways to do this task using Python: Delet 5 min read Like