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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 19 May

    Remove Extension From Filename in Python

    Table of ContentsHow to Remove Extension From Filename in Python?Remove Extension From Filename in Python Using the os ModuleRemove Extension From Filename in Python using the pathlib moduleRemove Multiple File Extensions Using pathlib Module in PythonConclusionWas this post helpful? While programming in python, we often have to deal with file names. In this article, we […]

  • 19 May

    Get Directory Name From File Path in Python

    Table of ContentsHow to Get directory name from file path in python?Get Directory Name From File Path Using the os Module in PythonGet Directory Name From File Path Using the pathlib ModuleRemove FilnName from File PathRemove Filename From the File Path Using the OS Module in PythonRemove Filename From File Path Using the Pathlib Module […]

  • 19 May

    Print Current Directory in Python

    Table of ContentsFiles and Directories in PythonWays to print the Current Directory in PythonUsing the os.getcwd() function to print the current directory in PythonUsing the os.path module to print the current directory in PythonUsing the os.path.abspath() function to print the current directory in PythonUsing the pathlib.Path.cwd() function to print the current directory in PythonUsing the […]

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusionWas this post helpful? In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file ConclusionWas this post helpful? We often use files to store the outputs of our programs in permanent storage. Generally, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.