Python list files in directory

In this post, we will see how to list all files in a directory in Python.

There are multiple ways to list all files in a directory in Python.

Using os.walk

Python os module provides multiple function to get list of files.

List all files in directory and its subdirectories using os.walk(path).
It iterates directory tree in path and for each directory, it returns a tuple with ( ,, )

Output:

/users/apple/temp/sample1.txt
/users/apple/temp/sample2.txt
/users/apple/temp/images/image1.jpeg
/users/apple/temp/images/image2.jpeg

List all .jpeg files in directory and its subdirectories using os.walk(path).

Output:

/users/apple/temp/images/image1.jpeg
/users/apple/temp/images/image2.jpeg

Using os.listdir(path)

You can also use os.listdir(path)to get list of files and subdirectories in a directory. In case, we get subdirectory while iterating, then we call getListOfFiles(path) recursively.

Here is the implementation of recursive function getListOfFiles(path).

Call the above function by passing path.

/users/apple/temp/sample1.txt
/users/apple/temp/sample2.txt
/users/apple/temp/images/image1.jpeg
/users/apple/temp/images/image2.jpeg

Using glob

You can use glob module to list files in directory and its subdirectory. It supports recursive globs using **.
List all files and subdirectories in a directory using glob.glob().

/users/apple/temp/sample1.txt
/users/apple/temp/sample2.txt
/users/apple/temp/images/image1.jpeg
/users/apple/temp/images/image2.jpeg

List all .txt files in directory and its subdirectory

Output:

/users/apple/temp/sample1.txt
/users/apple/temp/sample2.txt

List name of all subdirectories in a directory.

Output:

/users/apple/temp/
/users/apple/temp/images

Conclusion

We have learnt 3 ways to list all files in a directory in Python. We have also learnt about filtering files on the basis of extension.

Related Posts

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write a list to a file in Python
    12 June

    Write a list to a file in python

    Table of ContentsUsing the write() function to write a list to a file in PythonUsing the writelines() function to write a list to a file in PythonUsing the pickle.dump() function to write a list to a file in PythonUsing the json.dump() function to write a list to a file in PythonUsing the unpack operator * […]

  • Get filename from path in Python
    21 May

    Get filename from Path in Python

    Table of ContentsGet filename from Path in Python using the ntpath ModuleWindowsLinuxGet filename from Path in Python using the os.path.basename() FunctionGet filename from Path in Python using the os.path.split() FunctionGet filename from Path in Python using the pathlib.path().name MethodConclusion There are different ways to define the path of the file. In Windows, the path separator […]

  • Python read text file line by line
    13 May

    Read text file line by line in Python

    Learn about how to read text file line by line in Python.

  • 23 March

    Python write to text file

    Table of ContentsCreate txt file(‘x’)Write to txt file(‘w’)Append to txt file(‘a’) In this post, we will see how to write to a text file. There are multiple modes in which you can write to text file. Create txt file(‘x’) It will open the file in 'X' mode which will create new file and return error […]

  • 23 March

    Python read file into string

    Table of ContentsUsing read() methodUsing pathlib In this post, we will see how to read a file into a string variable. There are multiple ways to read file into String in Python. Using read() method Here are the steps to read file into string in python. Open the file in read mode using open() method […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.