Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:18:19+00:00 2026-06-02T20:18:19+00:00

I am writing a script to list the 20 largest files in a target

  • 0

I am writing a script to list the 20 largest files in a target directory. Once I have the files, I perform some math on the size to apply the correct human readable sizing information, i.e., Kb, Mb, Gb.

This however is getting the sort out of order. How can I do this, and keep the sort order intact?

#! /usr/bin/env python

import operator, os, sys

args = sys.argv
if len(args) != 2:
    print "You must one enter one directory as an argument."
    sys.exit(1)
else:
    target = args[1]

data = {}
for root, dirs, files in os.walk(target):
   for name in files:       
       filename = os.path.join(root, name)
       if os.path.exists(filename):
            size = float(os.path.getsize(filename))
            data[filename] = size

sorted_data = sorted(data.iteritems(), key=operator.itemgetter(1), reverse=True)
total = str(len(sorted_data))

while len(sorted_data) > 20:
    sorted_data.pop()

final_data = {}
for name in sorted_data:
    size = str(name[1])
    if size >= 1024:
        size = round(float(size) / 1024, 2)
        if size >= 1024:
            size = round(size / 1024, 2)
            if size >= 1024:
                size = round(size / 1024, 2)
                size = str(size) + "Gb"
            else:
                size = str(size) + "Mb"
    else:
        size = str(size) + "Kb"
    final_data[name] = size

print "The 20 largest files are:\n"
for name in final_data:
    print str(final_data[name]) + " " + str(name)
print "\nThere are a total of " + total + " files located in " + target
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:18:21+00:00Added an answer on June 2, 2026 at 8:18 pm

    Your problem is that you create a brand new dictionary to store the modified filesize data. Because that dictionary doesn’t contain any information about the file sizes, and because dictionaries don’t store their information in any fixed order, you lose your sort order. But it’s simple to recover; simply iterate over the sorted_data instead of the over the final_data, using final_data to access the human-readable file sizes. So something like this:

    for filename, size in sorted_data:
        print filename, final_data[filename]
    

    But an even better solution would be to put your human-readable string generating code into a function!

    def human_readable_size(size):
        # logic to convert size
        return hr_size
    

    Now you don’t even have to create a dictionary:

    for filename, size in sorted_data:
        print filename, human_readable_size(size)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script that takes a list of files from a directory, opens
I have seen majority of web programmers (writing some script with sql to make
I am writing a Jython script to sort a list of URLs. I have
I'm writing a python script to loop through a directory of CSS files and
hi I'm writing a script to loop through the current directory and list all
I'm writing a script to ssh in to a list of machines and compare
I'm writing a script that parses the pure-ftpwho -s command to get a list
I'm writing script in remote.ini The script looks like on 1:start:{ server some.irc.server server
I was writing a script on Windows Vista to move the files in a
I am writing a script that will look in a custom reports directory, copy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.