Check if directory contains files using python - GeeksforGeeks

29 Dec, 2020
Finding if a directory is empty or not in Python can be achieved using the listdir() method of the os library. OS module in Python provides functions for interacting with the operating system. This module provides a portable way of using operating system dependent functionality.
Syntax: os.listdir(<directory path>)
Returns: A list of files present in the directory, empty list if the directory is empty
Now by calling listdir() method, we can get a list of all files present in the directory. To check the emptiness of the directory we should check the emptiness of the returned list. We have many ways to do that, let us check them one by one.
By comparing the returned list with a hardcoded empty listAn empty list can be written as []. So we can compare the returned list’s equalness with [].# Python program to check# if a directory contains file    import os  # path of the directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"  # Comparing the returned list to empty listif os.listdir(directoryPath) == []:        print("No files found in the directory.")    else:        print("Some files found in the directory.")Output:Some files found in the directory.
By comparing length of the returned list with 0We can get length of a list by using len() method of Python. If the length of the returned list is equal to zero then the directory is empty otherwise not.# Python program to check if# a directory contains file    import os  # path of the directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"  # Checking the length of listif len(os.listdir(directoryPath)) == 0:        print("No files found in the directory.")    else:        print("Some files found in the directory.")Output:Some files found in the directory.
By comparing the boolean value of the listIn the above method, we used explicit comparison of the length of the list. Now we are heading with a more Pythonic way using truth value testing. An empty list is evaluated as False in Python.# Python program to check if# a directory is empty    import os  # path of the directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"  # Checking the boolean value of listif not os.listdir(directoryPath):        print("No files found in the directory.")    else:        print("Some files found in the directory.")Output:Some files found in the directory.

By comparing the returned list with a hardcoded empty listAn empty list can be written as []. So we can compare the returned list’s equalness with [].# Python program to check# if a directory contains file    import os  # path of the directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"  # Comparing the returned list to empty listif os.listdir(directoryPath) == []:        print("No files found in the directory.")    else:        print("Some files found in the directory.")Output:Some files found in the directory.

# Python program to check# if a directory contains file    import os  # path of the directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"  # Comparing the returned list to empty listif os.listdir(directoryPath) == []:        print("No files found in the directory.")    else:        print("Some files found in the directory.")
Output:
Some files found in the directory.

By comparing length of the returned list with 0We can get length of a list by using len() method of Python. If the length of the returned list is equal to zero then the directory is empty otherwise not.# Python program to check if# a directory contains file    import os  # path of the directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"  # Checking the length of listif len(os.listdir(directoryPath)) == 0:        print("No files found in the directory.")    else:        print("Some files found in the directory.")Output:Some files found in the directory.

# Python program to check if# a directory contains file    import os  # path of the directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"  # Checking the length of listif len(os.listdir(directoryPath)) == 0:        print("No files found in the directory.")    else:        print("Some files found in the directory.")
Output:
Some files found in the directory.

By comparing the boolean value of the listIn the above method, we used explicit comparison of the length of the list. Now we are heading with a more Pythonic way using truth value testing. An empty list is evaluated as False in Python.# Python program to check if# a directory is empty    import os  # path of the directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"  # Checking the boolean value of listif not os.listdir(directoryPath):        print("No files found in the directory.")    else:        print("Some files found in the directory.")Output:Some files found in the directory.

# Python program to check if# a directory is empty    import os  # path of the directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"  # Checking the boolean value of listif not os.listdir(directoryPath):        print("No files found in the directory.")    else:        print("Some files found in the directory.")
Output:
Some files found in the directory.

# Python program to check if# the directory is empty  import os    # Function for checking if the directory# containes file or notdef isEmpty(directoryPath):      # Checking if the directory exists or not    if os.path.exists(directoryPath):          # Checking if the directory is empty or not        if len(os.listdir(directoryPath)) == 0:            return "No files found in the directory."        else:            return "Some files found in the directory."    else:        return  "Directory does not exist !"  # Driver's code  # Valid directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil"print("Valid path:", isEmpty(directoryPath))  # Invalid directorydirectoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil/GeeksforGeeks"print("Invalid path:", isEmpty(directoryPath))
Output:
Valid path: Some files found in the directory.
Invalid path: Directory does not exist !

SriHarshaBammidi
Python os-module-programs
python-os-module
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | Get unique values from a list
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Pandas dataframe.groupby()