Wednesday, December 25, 2024
Google search engine
HomeLanguagesHow to iterate over files in directory using Python?

How to iterate over files in directory using Python?

Directory also sometimes known as a folder are unit organizational structure in a system’s file system for storing and locating files or more folders. Python as a scripting language provides various methods to iterate over files in a directory.

Below are the various approaches by using which one can iterate over files in a directory using python:

Method 1: os.listdir()

This function returns the list of files and subdirectories present in the given directory. We can filter the list to get only the files using os.path.isfile() function:

Example:

Python3




# import required module
import os
# assign directory
directory = 'files'
 
# iterate over files in
# that directory
for filename in os.listdir(directory):
    f = os.path.join(directory, filename)
    # checking if it is a file
    if os.path.isfile(f):
        print(f)


Output:

Method 2: os.scandir()

This method is used to get an iterator of os.DirEntry objects corresponding to the entries in the directory given by specified path.

Example:

Python3




# import required module
import os
 
# assign directory
directory = 'files'
 
# iterate over files in
# that directory
for filename in os.scandir(directory):
    if filename.is_file():
        print(filename.path)


Output:

Method 3: pathlib module

We can iterate over files in a directory using Path.glob() function which glob the specified pattern in the given directory and yields the matching files. Path.glob(‘*’) yield all the files in the given directory 

Example:

Python3




# import required module
from pathlib import Path
 
# assign directory
directory = 'files'
 
# iterate over files in
# that directory
files = Path(directory).glob('*')
for file in files:
    print(file)


Output:

Method 4: os.walk()

We can also search for subdirectories using this method as it yields a 3-tuple (dirpath, dirnames, filenames).

  • root: Prints out directories only from what you specified.
  • dirs: Prints out sub-directories from the root.
  • files: Prints out all files from root and directories.

Python3




# import required module
import os
 
# assign directory
directory = 'files'
 
# iterate over files in
# that directory
for root, dirs, files in os.walk(directory):
    for filename in files:
        print(os.path.join(root, filename))


Output:

Method 5: glob module

The glob.iglob() function returns an iterator over the list of pathnames that match the given pattern.

Example:

Python3




# import required module
import glob
 
# assign directory
directory = 'files'
 
# iterate over files in
# that directory
for filename in glob.iglob(f'{directory}/*'):
    print(filename)


Output:

RELATED ARTICLES

Most Popular

Recent Comments