Skip to content
Related Articles

Related Articles

How to iterate over files in directory using Python?
  • Last Updated : 24 Jan, 2021

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required module
import os
# assign directory
directory = 'files'
  
# itrate 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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Method 5: glob module

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

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :