Python 3 – os.listdir() Method
In Python, a directory is a path-like object that represents a folder that contains other files and directories. The os module provides a method called os.listdir() that lists all the files and directories present in a particular directory.
The os.listdir() method returns a list of all files and directories in the specified path. We can either pass the path of the directory as a string or use the dot (.) to represent the current directory. Let’s take a look at some examples to understand how to use os.listdir().
Example 1 – List all files and directories in a directory
import os
path = './'
files = os.listdir(path)
print(files)
Output:
['file1.txt', 'file2.txt', 'directory1', 'directory2']
In the above example, os.listdir() method is used to list all the files and directories present in the current directory. The code first imports the os module and assigns the string ‘./’ to the variable path which represents the current directory. The os.listdir() method is then called with path as an argument and the returned list is assigned to files variable. Finally, the files variable is printed using the print statement.
Example 2 – List all files in a directory
import os
path = './'
files = os.listdir(path)
for file in files:
if os.path.isfile(os.path.join(path, file)):
print(file)
Output:
file1.txt
file2.txt
In the above example, os.listdir() method is used to list all the files and directories present in the current directory. The code first imports the os module and assigns the string ‘./’ to the variable path which represents the current directory. The os.listdir() method is then called with path as an argument and the returned list is assigned to files variable.
The code then loops through each file in the files list and checks whether the file is a regular file or not using the os.path.isfile() method. The os.path.join() method is used to join the path and file variable to form a complete file path. If the file is a regular file, the print() statement prints the file name.
Example 3 – List all directories in a directory
import os
path = './'
files = os.listdir(path)
for file in files:
if os.path.isdir(os.path.join(path, file)):
print(file)
Output:
directory1
directory2
In the above example, os.listdir() method is used to list all the files and directories present in the current directory. The code first imports the os module and assigns the string ‘./’ to the variable path which represents the current directory. The os.listdir() method is then called with path as an argument and the returned list is assigned to files variable.
The code then loops through each file in the files list and checks whether the file is a directory or not using the os.path.isdir() method. The os.path.join() method is used to join the path and file variable to form a complete file path. If the file is a directory, the print() statement prints the directory name.
Conclusion
The os.listdir() method is a useful method in Python that allows us to list all the files and directories present in a particular directory. We can either pass the path of the directory as a string or use the dot (.) to represent the current directory. We can filter the list to show only files or directories using the os.path.isfile() and os.path.isdir() methods respectively. With the help of os.listdir(), we can easily navigate and manipulate the file system in our Python programs.