In this article, I'll share how to list all files in a directory using Python.
Step 1: Import the necessary modules
import os
Step 2: Define the directory path
Replace 'your_directory_path'
with the path to the directory you want to list the files from.
directory_path = 'your_directory_path'
Step 3: Use os.listdir()
to get the list of files
file_list = os.listdir(directory_path)
Step 4: Print or process the file list
You can print the file names or perform further processing based on your requirements.
for file_name in file_list:
print(file_name)
Complete Code:
import os
# Step 2: Define the directory path
directory_path = 'your_directory_path'
# Step 3: Use os.listdir() to get the list of files
file_list = os.listdir(directory_path)
# Step 4: Print or process the file list
for file_name in file_list:
print(file_name)
Hope, it will help you. Thanks for your time.
Subscribe to the Email Newsletter