Python: Check if a directory is empty
In this tutorial, we will study how to check whether the director (dir) is empty or not in the python programming language. First of all, we will understand the background of Python. Further, we will focus on understanding the meaning of the directory. Then, we will proceed further to our main topic. We will understand our topic through two examples.
As we know of the nature of python, it is an extensively used, all-purpose, high-level programming language. The speciality of this language is user-friendliness. It aims to lower the burden on programmers by providing several built-in functions and modules inspecting whether a directory is vacant or not is one of its popular functionalities. The vacancy of a directory can be achieved by means of the os module available here. For the purpose of interaction with the operating system, The OS module in Python delivers functions which serve our purpose. The OS module belongs to Python’s standard utility modules. This module offers a transportable method of using the functions that are not independent of the operating system. The modules such as os and os.path comprise numerous functions related to the file system.
What is directory
Directories are a method used for managing the files on a computer. It includes storing, organizing, and separating files. The directory without a parent or a guardian is a root directory. The path defines the route to reach and fetch the file. The path consists of a mixture of names of a directory, and names of a folder separated by slashes ( / ) and colon( : ), and this results in the route of a file in the system.
Example of a directory-
Checking whether the directory is empty or not
To inspect if a directory is vacant or not, the methods named os.listdir() are taken into account. This method of the os module is used to fetch the list of all the documents and directories in the stated directory.
Let us see the details of the Parameters used above:
- path (optional): It refers to the path of the directory
- Return Type: It refers to the method which returns the list of all documents and directories in the stated path. It returns the output in the list.
1.The directory is empty if the list given out by os.listdir() is empty; otherwise, it is filled. Below is the implementation to understand it.
# Python program to check if # the directory stated is empty or full. import os # giving the path of the directory path = "D:/Pycharm projects/tutorials and example/Sakshi" # Getting the list of directories dir = os.listdir(path) # Checking whether the list is empty or not if len(dir) == 0: print("The directory is empty ") else: print("The directory is not empty ")
1.The code written below is to overcome the issue of OSError, which can be encountered in the above program case. For example, if the path stated in the above code is a pathway to a text file or is a wrong path, then, in that circumstance, the above code will raise an OSError. So, to overcome this problem, following two methods, namely os. path.isfile() method and os.path.exists() method are taken into account. Below is the implementation to understand the stated concept.
# Python program to check if # the directory stated below is empty or filled. import os # The function to verify whether the path stated # is a valid directory or not. def isEmpty(path): if os.path.exists(path) and not os.path.isfile(path): # Checking whether the directory is empty or full if not os.listdir(path): print ("The directory is empty") else: print ("The directory is not empty ") else: print ("The path is either for a file or is invalid") # The path of a file path = "E:/monthly report/tutorials and example/Sakshi/jtp.txt" isEmpty(path) print () # valid path path = "E:/monthly report /tutorials and example/Sakshi/" isEmpty(path)
In this tutorial, we firstly understood the meaning of the directory and moved to our main topic, which is how to check whether the directory is empty or not. For this, we saw two snippets of code. The first one is a simple code, which could even result in an exception, while the second one overcomes the error possible in the first code.