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.
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.
- 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.
Enter folder path
The largest file is: /Users/tithighosh/Downloads/wordpress/wp-includes/js/dist/components.js
Size: 1792316 bytes
Enter folder path
The largest file is: /Users/tithighosh/Desktop/new/graph theory.pdf
Size: 64061656 bytes
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Rename all file names in your directory using Python
- Delete a directory or file using Python
- Python - Check if a file or directory exists
- Python: Check if a File or Directory Exists
- Python | Finding 'n' Character Words in a Text File
- Copy a directory recursively using Python (with examples)
- Get parent of current directory using Python
- Check if directory contains files using python
- Delete an entire directory tree using Python | shutil.rmtree() method
- reStructuredText | .rst file to HTML file using Python for Documentations
- Create a GUI to convert CSV file into excel file using Python
- Create a directory in Python
- Get directory of current Python script
- Python: Check if a directory is empty
- Change current working directory with Python
- Directory traversal tools in Python
- Implementing a Contacts directory in Python
- Python Directory Management
- How to Get directory of Current Script in Python?
- PYGLET – Opening file using File Location
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.