Largest file with Python

Find the Largest File in a Directory with Python

In this article we are going to discuss about finding a largest file in a directory by using Python OS module.

Debabrata Dash
2 min readJul 22, 2021

--

Photo by britt gaiser on Unsplash

OS module in Python is an inbuilt module, which exposed a number functions to interact with the operating system defendant functionality.

Below are the functions of OS module which we are used to find the largest file in a directory:

os.walk():

The folder path is taken as input. We then walk through the entire directory using os.walk() function.

Returns a tuple containing the root folder name, a list of subdirectories and a list of files.

os.stat()

It is used to get the status of the file and st_size attribute returns its size in bytes.

Example:

import os# folder path inputprint("Enter folder path")#It will return a absolute path name of the input passed to this functionpath = os.path.abspath(input())# for storing size of each filesize = 0

--

--

Debabrata Dash

At heart an entrepreneurial, learner, innovator. A dreamer with a passion to excel. Read all stories on medium.