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 496889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:43:44+00:00 2026-05-13T05:43:44+00:00

Example dump from the list of a directory: hello:3.1 GB world:1.2 MB foo:956.2 KB

  • 0

Example dump from the list of a directory:

hello:3.1 GB
world:1.2 MB
foo:956.2 KB

The above list is in the format of FILE:VALUE UNIT. How would one go about ordering each line above according to file size?

I thought perhaps to parse each line for the unit via the pattern “:VALUE UNIT” (or somehow use the delimiter) then run it through the ConvertAll engine, receive the size off each value in bytes, hash it with the rest of the line (filenames), then order the resulting dictionary pairs via size.

Trouble is, I have no idea about pattern matching. But I see that you can sort a dictionary

If there is a better direction in which to solve this problem, please let me know.


EDIT:

The list that I had was actually in a file. Taking inspiration from answer of the (awesome) Alex Martelli, I’ve written up the following code that extracts from one file, orders it and writes to another.

#!/usr/bin/env python

sourceFile = open("SOURCE_FILE_HERE", "r")
allLines = sourceFile.readlines()
sourceFile.close()

print "Reading the entire file into a list."

cleanLines = []

for line in allLines:
    cleanLines.append(line.rstrip())

mult = dict(KB=2**10, MB=2**20, GB=2**30)

def getsize(aline):
  fn, size = aline.split(':', 1)
  value, unit = size.split(' ')
  multiplier = mult[unit]
  return float(value) * multiplier

print "Writing sorted list to file."

cleanLines.sort(key=getsize)

writeLines = open("WRITE_OUT_FILE_HERE",'a')

for line in cleanLines:
    writeLines.write(line+"\n")

writeLines.close()
  • 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-05-13T05:43:44+00:00Added an answer on May 13, 2026 at 5:43 am
    thelines = ['hello:3.1 GB', 'world:1.2 MB', 'foo:956.2 KB']
    
    mult = dict(KB=2**10, MB=2**20, GB=2**30)
    
    def getsize(aline):
      fn, size = aline.split(':', 1)
      value, unit = size.split(' ')
      multiplier = mult[unit]
      return float(value) * multiplier
    
    thelines.sort(key=getsize)
    print thelines
    

    emits ['foo:956.2 KB', 'world:1.2 MB', 'hello:3.1 GB'], as desired. You may have to add some entries to mult if KB, MB and GB don’t exhaust your set of units of interest of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example from Using YAML with Python Original YAML file contains this # tree format
So ordinarily the element I want to pick from a list would be populated
I have a bash script mystuff containing a line like lynx -dump http://example.com >tmpfile
I'm trying to extract the subdomain from the HTTP_HOST value. However I've stumbled into
In Linux, supposing a thread's pid is [pid], from the directory /proc/[pid] we can
Very simple problem: I'm reading from one SocketChannel and would like to write the
I have a LIST (query output)that has the distinct ID’s from a table Account.
Basically i have to dump a series of temperature readings, into a text file.
I am trying to determine the best method of collecting a large list from
I am using WinDbg to load a crash dump from managed code (C#, a

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.