
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Directory Contains Files Using Python
Python is a versatile programming language that is widely used for various purposes, including file and directory operations. One of the most common tasks in file and directory operations is to check if a directory contains files or not. In this article, we will discuss how to check if a directory contains files using Python.
Before we dive into the implementation, let's understand the concept of directories and files.
A directory is a folder that contains files and other directories. It is used to organize files and directories in a hierarchical manner. In other words, a directory can contain other directories, which are called sub-directories.
A file is a collection of data that is stored on a computer. It can be a text file, an image file, an audio file, or any other type of file.
Now that we have a basic understanding of directories and files, let's discuss how to check if a directory contains files using Python.
Method 1: Using the os.listdir() method
The os module in Python provides a way to interact with the file system. The os.listdir() method is used to get a list of all the files and directories in a directory.
Here's how to use the os.listdir() method to check if a directory contains files
Example
import os dir_path = r'C:\Users\Leekha\Desktop' if len(os.listdir(dir_path)) == 0: print("Directory is empty") else: print("Directory contains files")
Output
After implementing the above lines of code, you will get the following output
Directory contains files
Let's see if the above can detect empty directory or not
We have created an empty folder in Desktop to check if the above code can detect it or not.
import os dir_path = r'C:\Users\Leekha\Desktop\Empty' if len(os.listdir(dir_path)) == 0: print("Directory is empty") else: print("Directory contains files")
Output
After implementing the above lines of code, you will get the following output
Directory is empty
In the above code, we first import the os module. Then we define the path to the directory we want to check in the dir_path variable.
We then use the os.listdir() method to get a list of all the files and directories in the directory. We check the length of the list using the len() function. If the length of the list is zero, it means the directory is empty. If the length of the list is greater than zero, it means the directory contains files.
Method 2: Using the glob.glob() method
The glob module in Python provides a way to search for files that match a specific pattern. The glob.glob() method is used to get a list of all the files that match a specific pattern.
Example
Here's how to use the glob.glob() method to check if a directory contains files
import glob dir_path = r'C:\Users\Leekha\Desktop' files = glob.glob(dir_path + '/*') if len(files) == 0: print("Directory is empty") else: print("Directory contains files")
Output
After implementing the above lines of code, you will get the following output
Directory contains files
Let's see if the above can detect empty directory or not
We have created an empty folder in Desktop to check if the above code can detect it or not.
import glob dir_path = r'C:\Users\Leekha\Desktop\Empty' files = glob.glob(dir_path + '/*') if len(files) == 0: print("Directory is empty") else: print("Directory contains files")
Output
After implementing the above lines of code, you will get the following output
Directory is empty
In the above code, we first import the glob module. Then we define the path to the directory we want to check in the dir_path variable.
We then use the glob.glob() method to get a list of all the files in the directory that match the pattern defined in the argument of the method. In this case, we are using the pattern '/*' to match all the files in the directory.
We check the length of the list using the len() function. If the length of the list is zero, it means the directory is empty. If the length of the list is greater than zero, it means the directory contains files.
Method 3: Using the os.scandir() method
The os.scandir() method is used to get an iterator of all the files and directories in a directory. This method is more efficient than the os.listdir() method as it returns an iterator instead of a list.
Example
Here's how to use the os.scandir() method to check if a directory contains files
import os dir_path = r'C:\Users\Leekha\Desktop' if not any(os.scandir(dir_path)): print("Directory is empty") else: print("Directory contains files")
Output
After implementing the above lines of code, you will get the following output
Directory contains files
Let's see if the above can detect empty directory or not
We have created an empty folder in Desktop to check if the above code can detect it or not.
Example
import os dir_path = r'C:\Users\Leekha\Desktop\Empty' if not any(os.scandir(dir_path)): print("Directory is empty") else: print("Directory contains files")
Output
After implementing the above lines of code, you will get the following output
Directory is empty
In the above code, we first import the os module. Then we define the path to the directory we want to check in the dir_path variable.
We use the os.scandir() method to get an iterator of all the files and directories in the directory. We use the any() function to check if there are any files in the iterator. If there are no files in the iterator, it means the directory is empty. If there are files in the iterator, it means the directory contains files.
Method 4: Using the os.path.isdir() method
The os.path module in Python provides a way to perform common operations on paths. The os.path.isdir() method is used to check if a given path is a directory.
Example
Here's how to use the os.path.isdir() method to check if a directory contains files
import os dir_path = r'C:\Users\Leekha\Desktop' if os.path.isdir(dir_path) and len(os.listdir(dir_path)) > 0: print("Directory contains files") else: print("Directory is empty")
Output
After implementing the above lines of code, you will get the following output
Directory contains files
Let's see if the above can detect empty directory or not
We have created an empty folder in Desktop to check if the above code can detect it or not.
import os dir_path = r'C:\Users\Leekha\Desktop\Empty' if os.path.isdir(dir_path) and len(os.listdir(dir_path)) > 0: print("Directory contains files") else: print("Directory is empty")
Output
After implementing the above lines of code, you will get the following output
Directory is empty
In the above code, we first import the os module. Then we define the path to the directory we want to check in the dir_path variable.
We use the os.path.isdir() method to check if the given path is a directory. If the path is a directory, we use the os.listdir() method to get a list of all the files and directories in the directory. We check the length of the list using the len() function.
If the length of the list is greater than zero, it means the directory contains files. If the length of the list is zero, it means the directory is empty.
Conclusion
In this tutorial, we discussed how to check if a directory contains files using Python. We covered four different methods to achieve this task.
The first method uses the os.listdir() method, the second method uses the glob.glob() method, the third method uses the os.scandir() method, and the fourth method uses the os.path.isdir() method.
All these methods are effective in their own way, and the choice of method depends on the specific use case.