Python — widely used universal high-level programming language. It provides many functions and one of them — checking if the directory is empty or not. This can be achieved with the OS module . The os.listdir () method os.listdir ()
os module method is used to get list all files and directories in the specified directory.
Syntax: os.listdir (path)
Parameters:
path (optional): path of the directory
Return Type: This method returns the list of all files and directories in the specified path ... The return type of this method is list.
Example # 1: If the list returned by os.listdir ()
is empty, then the directory is empty, otherwise not. Below is the implementation.
# Python program for checking # empty directory or not import os # path to directory path = "D: / Pycharm projects / Python.Engineering / Nikhil" # Get directory listing dir = os.listdir (path) # Check if the list is empty or not if len ( dir ) = = 0 : print ( "Empty directory" ) else : print ( " Not empty directory " ) |
Exit:
Empty directory
Example # 2. Suppose the path given in the above code is the path to a text file or the wrong way, then in this case Sometimes the above code will raise OSError
. To overcome this error, we can use os.path.exists ()
. Below is the implementation.
# Python checker # is the directory empty or not import os # Function to check if the path is specified # correct directory specified def isEmpty (path): if os.path.exists (path) and not os.path.isfile (path): # Check if the directory is empty or not if not os.listdir (path): print ( "Empty directory" ) else : print ( "Not empty directory" ) else : print ( < code class = "string"> "The path is either for a file or not valid" ) # file path path = "D: / Pycharm projects / Python.Engineering / Nikhil / gfg. txt " isEmpty (path) print () # valid path path = "D: / Pycharm projects / Python.Engineering / Nikhil / " isEmpty (path) |
Exit :
The path is either for a file or not valid Not empty directory
Shop
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best laptop for development
$499+
Best laptop for Cricut Maker
$299+
Best laptop for hacking
$890
Best laptop for Machine Learning
$699+
Raspberry Pi robot kit
$150
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
606 answers
PythonStackOverflow
Python os.path.join () method
384 answers
PythonStackOverflow
Flake8: Ignore specific warning for entire file
360 answers
Wiki
Python | How to copy data from one Excel sheet to another
Common xlabel/ylabel for matplotlib subplots
Check if one list is a subset of another in Python
How to specify multiple return types using type-hints
Printing words vertically in Python
Python Extract words from a given string
Cyclic redundancy check in Python
Finding mean, median, mode in Python without libraries
Python add suffix / add prefix to strings in a list
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
Python - Move item to the end of the list
Python - Print list vertically