Skip to content →

Listing the top-size files with python

List Top File-size Files

This Python script is designed to list the top files by size on a file drive (under Windows-OS), as determined by the user.

Requirements

In order to use this script, you must have Python 3 installed on your system. You will also need to have the os and sys modules installed.

import os
# ask user for device id and number of files to list
while True:
device_id = input("Enter the device id (e.g. C:): ")
if os.path.isdir(device_id):
break
print("Invalid device id. Please enter a valid device id.")
while True:
num_files_str = input("Enter the number of files to list: ")
if not num_files_str.isdigit():
print("Invalid input. Please enter a positive integer.")
continue
num_files = int(num_files_str)
if num_files > 0:
break
print("Invalid input. Please enter a positive integer.")
# get list of files on device and sort by size
file_list = []
for root, dirs, files in os.walk(device_id):
for name in files:
try:
file_size = os.path.getsize(os.path.join(root, name))
file_list.append((os.path.join(root, name), file_size))
except:
# ignore file if it cannot be accessed
pass
file_list.sort(key=lambda x: x[1], reverse=True)
# print top n files and ask user to select a file to open
while True:
print("\nSelect a file to open:")
for i in range(num_files):
if i >= len(file_list):
break
file_path = file_list[i][0]
file_size = file_list[i][1]
print(f"{i+1}. {file_path} ({file_size} bytes)")
print("0. Exit")
# ask user to enter the index of the file to open
while True:
file_index_str = input("Enter the index of the file to open: ")
if not file_index_str.isdigit():
print("Invalid input. Please enter a positive integer.")
continue
file_index = int(file_index_str)
if 0 <= file_index <= num_files:
break
print(f"Invalid input. Please enter a number between 0 and {num_files}.")
# exit if user enters 0
if file_index == 0:
break
# open explorer window for the selected file
file_path = file_list[file_index-1][0]
os.startfile(os.path.dirname(file_path))

Usage

  1. Run the script from the command line using python listTopFilesizedFiles.py.
  2. Enter the path of the drive you would like to search when prompted.
  3. Enter the number of files you would like to see listed when prompted.
  4. Choose an action from the menu that appears:
    • Enter a file number to open that file’s location in Windows Explorer
    • Enter 0 to exit the program
    • Enter r to run the script again from the beginning
  5. If you choose to open a file location, a Windows Explorer window will open to the directory containing the selected file.
  6. If you choose to exit the program, the script will end.
  7. If you choose to run the script again, you will be prompted to enter a new drive path and number of files to list.

Example

 
python listTopFilesizedFiles.py
Enter the path of the drive you want to search:
C:\Users

Enter the number of files to list: 10

 

Top 10 biggest files in C:\Users:

 

1. File1.txt - 500 MB
2. File2.exe - 300 MB
3. File3.pdf - 200 MB
4. File4.mp4 - 150 MB
5. File5.ppt - 100 MB
6. File6.xlsx - 90 MB
7. File7.docx - 80 MB
8. File8.iso - 70 MB
9. File9.dll - 60 MB

10. File10.zip - 50 MB

 

What would you like to do?
Enter a file number to open its location,
enter 0 to exit, or enter r to start over: 2
Opening File2.exe location in Windows Explorer...

License

This script is released under the MIT License. Feel free to use it however you like.

Download

You can download the sourcecode and find more details on my github-repository:
https://github.com/smartDevel/list_Top_Size_Files

Click to rate this post!
[Total: 0 Average: 0]

Published in Coding Computers and IT Start

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.