How to List Files In Directory in python- Detailed Guide

Listing files in a directory is useful to check the files available in the directory.

You can list files in a directory in python using the os.listdir() method.

In this tutorial, you’ll learn how to list files in a directory in python using the different libraries.

If you’re in Hurry

You can use the below code snippet to list files in a directory.

os.listdir() lists all the files and folders in the directory. If a path is not given, then it lists the files from the current working directory.

import os

arr = os.listdir("c:/temp/")

print("\n".join(arr))

Use only forward slash in the directory path location.

Output

    csvfile_0.csv
    csvfile_1.csv
    sample_dataframe.csv
    sample_dataframe_Float_Two_Decimals.csv
    Test_Folder

The highlighted object is the folder in the directory.

Listing Only Files in a Directory

To display only the files in a directory, you can use the os.walk() method.

It’ll return two separate lists of files and folders. You can iterate the files list and access the files in the directory.

Snippet

import os

for (root, dirs, file) in os.walk("c:/temp/"):
    for f in file:
        print(f)

Output

csvfile_0.csv
csvfile_1.csv
sample_dataframe.csv
sample_dataframe_Float_Two_Decimals.csv

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods available to list files in a directory.

There are five methods available to List files in a directory. You’ll learn how to use these methods and also learn how to use these methods for different use-cases.

Using listdir()

listdir fetches all files and folders in the directory.

It lists all files and folders in the current directory.

Snippet

import os

arr = os.listdir()

print("\n".join(arr))

Output

.ipynb_checkpoints
09_Add_Column_to_dataframe_pandas.ipynb
10_Change_column_type_in_pandas.ipynb
supermarket_sales.csv
temp.html
Untitled.ipynb

If you want to list files from a custom directory, then you can pass the directory name to the listdir() method as shown below.

Snippet

import os

arr = os.listdir("c:/temp/")

#print(arr)

print("\n".join(arr))

Output

csvfile_0.csv
csvfile_1.csv
sample_dataframe.csv
sample_dataframe_Float_Two_Decimals.csv
Test_Folder

This is how you can use the listdir() method.

Using os.walk()

os.walk() method can be used to list the files and folders in a directory.

This method walks through the directory either top-down or bottom-up and returns the list of the entries.

To walk through from top to bottom, you can pass the parameter to topdown=True.

For more details, refer the doc.

Snippet

import os

for (root, dirs, file) in os.walk("c:/temp/"):
    for f in file:
        print(f)

You can see the below output. The entries in the directory are printed in a top to bottom order.

Output

    csvfile_0.csv
    csvfile_1.csv
    csvfile_2.csv
    sample_dataframe.csv
    sample_dataframe_Float_Two_Decimals.csv
    test.txt

This is how you can use the os.walk() method.

Using os.scandir()

You can use the os.scandir() method to list the entries in the directory.

The os.scandir() returns the iterator of the directory objects. Then it can be iterated and printed as follows.

Snippet

In the example,

  • First, you’ll generate the iterator of the entries available in the directory
  • Use a for loop to iterate the entries.
  • Each entry name can be printed using the entry.name attribute.
import os

dirpath = os.getcwd()

listOfEntries = os.scandir(dirpath)

for entry in listOfEntries:

    if entry.is_file():
        print(entry.name)

You’ll see all the available entries in the directory printed as below.

Output

     .ipynb_checkpoints
    09_Add_Column_to_dataframe_pandas.ipynb
    10_Change_column_type_in_pandas.ipynb
    ask_vikram_contents
    supermarket_sales.csv
    temp.html
    Untitled.ipynb
    Untitled1.ipynb

This is how you can scan the directory and iterate the entries to print its names.

Using Pathlib

You can use the PathLib library to list all files from a directory. For more details, refer to the pathlib doc.

  • First, create an object using the directory from which you want to list files.
  • With the object, iterate the directory using the iterdir() method and print each file entry.

This will print all the files and just the sub-directories available in the main directory. It’ll not print the files available in the sub-directories.

Snippet

import pathlib

currentDirectory = pathlib.Path('c:/temp/')

for currentFile in currentDirectory.iterdir():
    print(currentFile)

You’ll see the file names and the subdirectory names.

Output

    c:\temp\csvfile_0.csv
    c:\temp\csvfile_1.csv
    c:\temp\csvfile_2.csv
    c:\temp\sample_dataframe.csv
    c:\temp\sample_dataframe_Float_Two_Decimals.csv
    c:\temp\Test_Folder

This is how you can use Pathlib to list the files in a directory.

Using glob

You can use the glob API to list files using Pattern matching or regular expressions.

It is useful when you don’t know the exact file names or directory names, but you want to check if a file exists with such a pattern.

You can also check the files in the sub-directories using the flag recursive.

  • If True, sub directories will be included.
  • If False, sub directories will be ignored.

Snippet

import glob

for file in glob.iglob('c:/temp/*', recursive=True):
    print(file)

Output

c:/temp\csvfile_0.csv
c:/temp\csvfile_1.csv
c:/temp\csvfile_2.csv
c:/temp\sample_dataframe.csv
c:/temp\sample_dataframe_Float_Two_Decimals.csv
c:/temp\Test_Folder

Next, let’s discuss the use-cases of using GLOB.

List Files In Directory And Subdirectories

In this section, you’ll learn how to list all files in directories and subdirectories.

You can do this by using the os.walk() method.

  • It walks through all the entries in a directory.
  • If any subdirectory is found, it also walks through the entries available in the subdirectory.
  • Finally, it yields separate tuples which contain directory paths, subdirectories, and files.

You can iterate over the files tuple to access the file entries in the directory.

Snippet

import os

for path, subdirs, files in os.walk("c:/temp/"):
    for name in files:
        print(os.path.join(path, name))

You’ll see all the files printed in the main directory and the subdirectory.

Output

c:/temp/csvfile_0.csv
c:/temp/csvfile_1.csv
c:/temp/csvfile_2.csv
c:/temp/sample_dataframe.csv
c:/temp/sample_dataframe_Float_Two_Decimals.csv
c:/temp/Test_Folder\test.txt

This is how you can print the files in a directory and sub-directories.

List Files In Directory Full Path

In this section, you’ll learn how to list files in a directory with full path information. Similar to the previous section,

  • Walk through the directory using the os.walk() method and obtain separate tuples with path, sub-directory, and the files information.
  • To list the files with a full path, you can use the os.path.join() method to join the path and the name of the file as shown below.

Snippet

for path, subdirs, files in os.walk("c:/temp/"):
    for name in files:
        print(os.path.join(path, name))

You’ll see the below output. Filenames will be printed with the full path information.

Output

c:/temp/csvfile_0.csv
c:/temp/csvfile_1.csv
c:/temp/csvfile_2.csv 
c:/temp/sample_dataframe.csv
c:/temp/sample_dataframe_Float_Two_Decimals.csv
c:/temp/Test_Folder\test.txt

This is how you can list files with full path information.

List Files In Directory With Pattern Matching

In this section, you’ll learn how to list files in a directory with pattern matching.

This will be useful when you don’t know the exact file name but want to find files in a specific pattern.

You can use the regex to list files in a directory.

For example, let’s learn how to list down the files starting with the name sample using the glob library.

Starting With

The regex for finding files starting with the text sample is sample*.

This means the file name should start with the text sample, and after that, it shall contain any set of characters.

Snippet

Use the below snippet to list files starting with sample

import glob

print(glob.glob("c:/temp/sample*"))

There are five files starting with the text sample. All these files will be printed.

Output

    ['c:/temp\\sample_dataframe.csv', 'c:/temp\\sample_dataframe_Float_Two_Decimals.csv', 'c:/temp\\sample_dataframe_Missing_Values.csv', 'c:/temp\\sample_dataframe_Tab_separator.csv',  'c:/temp\\sample_dataframe_With_Two_Columns.csv']

You can use any regex pattern along with glob to identify files with a specific pattern.

List Files In Directory Sorted By Name

In this section, you’ll learn how to list all files in a directory and display them in a sorted way.

List all entries in a specified directory using os.listdir().

Next, Using list comprehension,

  • Iterate the entries using file for file in os.listdir(dir_path)
  • Check if the entry is a file using if os.path.isfile(os.path.join(dir_path, file). If yes, then return the entry to the list comprehension. You’ll get a list of files.
  • Sort the list of files using the sorted() method.

Use the below snippet to list the files in a sorted way.

Snippet

import os

dir_path = 'C:/temp/'

list_of_files = sorted(file for file in os.listdir(dir_path) 
                           if os.path.isfile(os.path.join(dir_path, file)))


for file_name in list_of_files:
    print(file_name)

You’ll see the file names listed in an alphabetically sorted way.

Output

    csvfile_0.csv
    csvfile_1.csv
    csvfile_2.csv
    sample_dataframe.csv
    sample_dataframe_Float_Two_Decimals.csv

You’ve displayed files in the directory sorted by name.

List Files In Directory and Subdirectories Sorted By Name

In this section, you’ll learn how to list files in directory and subdirectories sorted by their name.

To display the files in a directory and its subdirectory, Use

  • glob.glob(dir_path + '/**/*', recursive=True) to return all the entries available in the directory.
  • filter only the file type entries using the filter() method and condition os.path.isfile. With this the sub-directory names will be ignored in the list. But the files in the sub-directories will be added to the list.
  • Sort the list of file names using the sorted() method.

Snippet

import glob

import os

dir_path = 'C:/temp/'


list_of_files = sorted(filter( os.path.isfile,
                        glob.glob(dir_path + '/**/*', recursive=True) ) )

for file_path in list_of_files:
    print(file_path) 

Output

You’ll see the output displayed below.

  • First, the files in the directory will be displayed
  • Then the files in the main directory will be displayed in a sorted way.
    C:/temp\Test_Folder\test.txt
    C:/temp\csvfile_0.csv
    C:/temp\csvfile_1.csv
    C:/temp\csvfile_2.csv
    C:/temp\sample_dataframe.csv
    C:/temp\sample_dataframe_Float_Two_Decimals.csv

This is how you can list files sorted by using its name.

Conclusion

To summarize, you’ve learned the different methods available in python to list files in a directory. You’ve also learned how those methods can be used in various scenarios to display the file names is available in the directory.

If you want to list files from directories from the system shell read How to execute a shell command from Python tutorial.

If you have any questions, comment below.

You May Also Like

Leave a Comment