Welcome, guest | Sign In | My Account | Store | Cart

A simple (4 lines) recipe that will sort a file named on the command line and write the sorted lines to standard output.

Python, 16 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import fileinput

def sort_file():
    """ Sort the lines in the file named on standard input,
    outputting the sorted lines on stdout.

    Example call (from command line)
    sort_file.py unsorted.txt > sorted.txt
    """
    lines=[] # give lines variable a type of list
    for line in fileinput.input(): lines.append(line.rstrip())
    lines.sort()
    for line in lines: print line

if __name__ == "__main__":
    sort_file()

Sorting a file is a common task for scripting languages. Using the fileinput module and the builtin capabilities of lists it can be done easily in Python.

An issue with the code as presented is that it doesn't check the filename that the user gives on the command line. If the name cannot be opened an IO error is thrown and you get an ugly traceback. It might also be more generally useful if the user were allowed to input a wildcard filename and the glob module were used to expand the wildcard to a list of files to be sorted. This would bring up the question of whether the output should be a sort of the concatenation of the files or whether each file should be sorted seperately. The point of this recipe is to show a simple task done quickly so I have left these elaborations off.

2 comments

Kevin Schluff 18 years, 6 months ago  # | flag

Single file approach. Be aware that using fileinput.input() will concatenate all of the files on the command line into the output. I'm not sure if that's what the recipe intended. A slightly shorter version that works on a single file is:

import sys

lines = open(sys.argv[1]).readlines()
lines.sort()
map(sys.stdout.write, lines)

There might even be a shorter way to write this. Any takers?

Markus Weihs 18 years, 6 months ago  # | flag

Hi!

With sorted() you can make this a oneliner:

map(sys.stdout.write, sorted(file(sys.argv[1]).readlines()))

Regards, Markus

Created by Phil Robare on Wed, 28 Sep 2005 (PSF)
Python recipes (4591)
Phil Robare's recipes (1)

Required Modules

Other Information and Tasks