Python: List of Files in Directory and Subdirectories

Python: List of Files in Directory and Subdirectories

Python has inbuilt os and glob module that provides function like os.walk(), os.listdir(), blob.blob() method that list directories and subdirectories.

Python has an OS module that provides the functions to deal with file management. We can list files in directory and subdirectory programmatically in Python using the OS module. In this article, we will discuss the different methods to generate a list of all files in the directory tree.

Python: List of Files in Directory and Subdirectories

Let’s print the whole files listed in our current working directory.

First, you need to import the os module in your file and then use the os.listdir() function.

Write the following code inside the * *file.


import os


python os module

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

The Basics of Python OS Module

The OS module is a python module that provides the interface for interacting with the underlying operating system that Python is running.

Python Modules: How To Create Modules in Python

Python modules are files containing the set of functions you want to include in your app. Create a module and import that module in your file.

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

How to Use OS Module in Python

In this tutorial, we'll learn about OS Module and how can you use it in variety of operating system related tasks using it’s methods