In this example, we are going to explore 5 ways to List files in Directory in Python as well as in a subdirectory with examples. There are built-in methods like os.listdir(), os.walk(), os.scandir(), pathlib.iterdir(), glob() in Python to List files in the directory.
1. Using os.listdir() method
The Python OS module method os.listdir(path) returns a list of all files and directories in the directory given by Path. The return list is in arbitrary order. The os module provides functions to handle the directory and files. So we need to import the OS module in code using import os.
Syntax
os.listdir(path)
Parameters
- Path: It is the path of a directory in which files we need to list. By default, it is the current directory.
Program example: Get the list of files and directories.
import os
path = os.getcwd()
list_dir_files = os.listdir(path)
print('List file and direcories at path :',path)
# list all files
print(list_dir_files)
Output
List file and direcories at path : C:\Users\Admin\Desktop\Python_program
['car.png', 'config', 'data', 'devenum.txt', 'File.txt', 'geek.txt', 'lang.txt', 'lang1.txt', 'myqr.png','students.csv', 'studentTable.csv', 'test']
1.2 .Get list of .csv file using os.listdir()
We can get a list of specific extension files from a directory. In this below example we are getting .csv files in a directory.
Program to get list of .csv file using os.listdir()
import os
path = os.getcwd()
print('List file and direcories at path :',path)
for file in os.listdir(path):
if file.endswith(".csv"):
# get list of .csv file present gievn folder
print(file)
Output
List file and direcories at path : C:\Users\Admin\Desktop\Python_program
students.csv
studentTable.csv
1.3 Get all files Recursively Using os.listdir()
To Get the list of files in the Directory and subdirectory recursively using the os.listdir() method, We have defined a custom function to achieve this.
Program Example get files Recursively
import os
path = os.getcwd()
def getAllFiles(path):
filesList = list()
for file in os.listdir(path):
#file full path
absolute_path = os.path.join(path,file)
if os.path.isfile(absolute_path):
filesList.append(absolute_path)
else:
filesList = filesList+getAllFiles(absolute_path)
return filesList
#calling the above define custome function
print(getAllFiles(path))
Output
'C:\\Users\\Admin\\Desktop\\Python_program\\car.png', 'C:\\Users\\Admin\\Desktop\\Python_program\\config\\blacklist.txt', 'C:\\Users\\Admin\\Desktop\\Python_program\\config\\comments.txt', 'C:\\Users\\Admin\\Desktop\\Python_program\\config\\followed.txt']
2. Using os.walk()
os.walk() method fetches a list of files in directories and subdirectories. It iterates over the directory tree and each directory in the tree.
import os
path = os.getcwd()
for (directory, subdirs, allfile) in os.walk(path):
for file in allfile:
print('filename:',file)
print('\n File_Path\n:',os.path.join(directory,file))
Output
filename: friends.txt
File_Path : C:\Users\Admin\Desktop\Python_program\config\friends.txt
filename: car.png
File_path : C:\Users\Admin\Desktop\Python_program\car.png
2.1 Os.walk() to get list of files based on extension
We can use the os.walk() method to access files based on extension from a directory. The extensions can be (.csv,.png, jpeg,.py), etc.
Program Example
import os
path = os.getcwd()
for (directory, subdirs, allfile) in os.walk(path):
for file in allfile:
if '.txt' in file:
print(file)
Output
devenum.txt
File.txt
geek.txt
lang.txt
lang1.txt
3. Using os.scandir()
The os.scandir() method is available in Python 3.6, It returns an iterator of os.dirEntry object. It is a simple method to list files in a directory.
Program Example
import os
path = os.getcwd()
#list of files and directories
with os.scandir(path) as list_of_dir_files:
for entry in list_of_dir_files:
#list of files
if entry.is_file():
print(entry.name)
Output
car.png
devenum.txt
students.csv
studentTable.csv
4. Using glob module
The glob module can be used to list the files in the directory and subdirectory. We can use recursive glob using **.
Program Example
import glob
import os
path = os.getcwd()
Pattern = "**/*"
for file in glob.glob(path +Pattern, recursive=True):
print(file)
Output
C:\Users\Admin\Desktop\Python_program\car.png
C:\Users\Admin\Desktop\Python_program\config
C:\Users\Admin\Desktop\Python_program\data
4.1.Get list of .txt files in a directroy using glob
We can use the glob() method to access files based on extension from a directory. The extension can be (.csv,.png, jpeg,.py), etc.
import glob
import os
path = os.getcwd()
Pattern = "**/*.txt"
for file in glob.glob(path +Pattern, recursive=True):
print(file)
Output
C:\Users\Admin\Desktop\Python_program\devenum.txt
C:\Users\Admin\Desktop\Python_program\File.txt
C:\Users\Admin\Desktop\Python_program\geek.txt
C:\Users\Admin\Desktop\Python_program\lang.txt
5. Using Pathlib
We can use pathlib module to list all files in a directory. It provides classes that represent the file system path. We can use pathlib.iterdir() method.
Program Example
import pathlib
# geting path of current directory
currdir = pathlib.Path('.')
for File in currdir.iterdir():
print(File)
Output
car.png
config
data
devenum.txt
File.txt
geek.txt
lang.txt
Summary :
We have explored 5 different ways to List file in Directory with code example by Using different methods like os.listdir(),os.walk(),os.scandir(), pathlib.iterdir(),glob().