Check if directory contains files using python Last Updated : 12 Dec, 2022 Comments Improve Suggest changes Like Article Like Report 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 list An empty list can be written as []. So we can compare the returned list's equalness with []. Python3 # Python program to check # if a directory contains file import os # path of the directory directoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil" # Comparing the returned list to empty list if 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 0 We 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. Python3 # Python program to check if # a directory contains file import os # path of the directory directoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil" # Checking the length of list if 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 list In 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. Python3 # Python program to check if # a directory is empty import os # path of the directory directoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil" # Checking the boolean value of list if 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.Complete source code: Python3 # Python program to check if # the directory is empty import os # Function for checking if the directory # contains file or not def 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 directory directoryPath = "D:/Pycharm projects/GeeksforGeeks/Nikhil" print("Valid path:", isEmpty(directoryPath)) # Invalid directory directoryPath = "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 ! Comment More infoAdvertise with us Next Article Check if directory contains files using python S SriHarshaBammidi Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2019 python-os-module Python os-module-programs +1 More Practice Tags : python Similar Reads List all files of certain type in a directory using Python In python, there are several built-in modules and methods for file handling. These functions are present in different modules such as os, glob, etc. This article helps you find out many of the functions in one place which gives you a brief knowledge about how to list all the files of a certain type 3 min read Get parent of current directory using Python In Python, the OS module is used to interact with the operating system. It comes under Python's standard utility modules and provides a portable way of using OS-dependent functionality. The os and os.path modules include many functions to interact with the file system. OS module provides various way 3 min read Python - List Files in a Directory Sometimes, while working with files in Python, a problem arises with how to get all files in a directory. In this article, we will cover different methods of how to list all file names in a directory in Python.Table of ContentWhat is a Directory in Python?How to List Files in a Directory in PythonLi 8 min read Deleting Duplicate Files Using Python In this article, we are going to use a concept called hashing to identify unique files and delete duplicate files using Python. Modules required:tkinter: We need to make a way for us to select the folder in which we want to do this cleaning process so every time we run the code we should get a file 5 min read How to print all files within a directory using Python? The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to 3 min read Python - Read file from sibling directory In this article, we will discuss the method to read files from the sibling directory in Python. First, create two folders in a root folder, and one folder will contain the python file and the other will contain the file which is to be read. Below is the dictionary tree: Directory Tree: root : | |__S 3 min read Python - How to Check if a file or directory exists Sometimes it's necessary to verify whether a dictionary or file exists. This is because you might want to make sure the file is available before loading it, or you might want to prevent overwriting an already-existing file. In this tutorial, we will cover an important concept of file handling in Pyt 5 min read Get Current directory in Python In this article, we will cover How to Get and Change the Working Directory in Python. While working with file handling you might have noticed that files are referenced only by their names, e.g. 'GFG.txt' and if the file is not located in the directory of the script, Python raises an error. The conce 3 min read Read all Files in Directory using R To list all files in a directory in R programming language we use list.files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories. If no files are present in the directory, 2 min read Python List All Files In Directory And Subdirectories Listing all files in a directory and its subdirectories is a common task in Python, often encountered in file management, data processing, or system administration. As developers, having multiple approaches at our disposal allows us to choose the most suitable method based on our specific requiremen 5 min read Like