Open In App

Finding the largest file in a directory using Python

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will find the file having the largest size in a given directory using Python. We will check all files in the main directory and each of its subdirectories.

Modules required:
os:
The os module in Python provides a way of using operating system dependent functionality. OS module is available with Python’s Standard Library and does not require installation.

Explanation:

  • The folder path is taken as input. We then walk through the entire directory using os.walk() function.
  • os.walk() returns a tuple containing the root folder name, a list of subdirectories and a list of files.
  • os.stat() is used to get the status of the file and st_size attribute returns its size in bytes.

Below is the implementation.




import os
  
  
# folder path input
print("Enter folder path")
path = os.path.abspath(input())
  
# for storing size of each 
# file
size = 0
  
# for storing the size of 
# the largest file
max_size = 0
  
# for storing the path to the 
# largest file
max_file =""
  
# walking through the entire folder,
# including subdirectories
  
for folder, subfolders, files in os.walk(path):
      
    # checking the size of each file
    for file in files:
        size = os.stat(os.path.join( folder, file  )).st_size
          
        # updating maximum size
        if size>max_size:
            max_size = size
            max_file = os.path.join( folder, file  )
  
print("The largest file is: "+max_file)
print('Size: '+str(max_size)+' bytes')


Output:

Input:
Enter folder path
/Users/tithighosh/Downloads/wordpress

Output:
The largest file is: /Users/tithighosh/Downloads/wordpress/wp-includes/js/dist/components.js
Size: 1792316 bytes

Input:
Enter folder path
/Users/tithighosh/Desktop

Output:
The largest file is: /Users/tithighosh/Desktop/new/graph theory.pdf
Size: 64061656 bytes


Previous Article
Next Article

Similar Reads

Finding Md5 of Files Recursively in Directory in Python
MD5 stands for Message Digest Algorithm 5, it is a cryptographic hash function that takes input(or message) of any length and produces its 128-bit(16-byte) hash value which is represented as a 32-character hexadecimal number. The MD5 of a file is the MD5 hash value computed from the content of that file. It's a unique representation of the file's c
3 min read
Rename all file names in your directory using Python
Given multiple files in a directory having different names, the task is to rename all those files in sorted order. We can use OS module in order to do this operation. The OS module in Python provides functions for interacting with the operating system and provides a portable way of using operating system-dependent functionality. We can go to the cu
1 min read
Delete a directory or file using Python
In this article, we will cover how to delete (remove) files and directories in Python. Python provides different methods and functions for removing files and directories. One can remove the file according to their need. Various methods provided by Python are - Using os.remove()Using os.rmdir()Using shutil.rmtree()Using pathlib.Path(empty_dir_path).
7 min read
Python: Check if a File or Directory Exists
Sometimes the need to check if the folder exists in python, and check whether a directory or file exists becomes important because maybe you want to prevent overwriting the already existing file, or maybe you want to make sure that the file is available or not before loading it. So to check how to check if a Directory Exists without exceptions in P
5 min read
Python - Read file from sibling directory
In this article, we will discuss the method to read files from the sibling directory in Python. First, create two folders in a root folder, and one folder will contain the python file and the other will contain the file which is to be read. Below is the dictionary tree: Directory Tree: root : | |__Sibling_1: | \__demo.py | |__Sibling_2: | \__file.t
3 min read
Get sorted file names from a directory by creation date in Python
In this article, we will understand how to retrieve sorted file names from a directory using Python. For this, we would make use of the Python glob library's glob function. There is no need to install this module externally because it is already included with Python. Firstly, The glob function would be used to obtain the list of files/directories w
3 min read
How to fix: "fatal error: Python.h: No such file or directory"
The "fatal error: Python.h: No such file or directory" error is a common issue encountered when compiling C/C++ code that interacts with Python. This error occurs when the C/C++ compiler is unable to locate the Python.h header file, which is part of the Python development package required for compiling code that interacts with Python.In this articl
5 min read
Filenotfounderror: Errno 2 No Such File Or Directory in Python
When working with file operations in programming, encountering errors is not uncommon. One such error that developers often come across is the FileNotFoundError with the Errno 2: No such file or directory message. This error indicates that the specified file or directory could not be found at the given path. In this article, we'll delve into the ca
3 min read
Python - How to Check if a file or directory exists
Sometimes it's necessary to verify whether a dictionary or file exists. This is because you might want to make sure the file is available before loading it, or you might want to prevent overwriting an already-existing file. In this tutorial, we will cover an important concept of file handling in Python about How to check if a file already exists in
5 min read
Python | Finding 'n' Character Words in a Text File
This article aims to find words with a certain number of characters. In the code mentioned below, a Python program is given to find the words containing three characters in the text file. Example Input: Hello, how are you ? , n=3 Output: how are you Explanation: Output contains every character of the of length 3 from the input file.Input File: File
3 min read
three90RightbarBannerImg