python script to inventorise your ab1 files with md5sums

November 7, 2008 at 11:35 am (opensource, tips) (, , , , )

The problem that needed solving this time was having a list of filenames of my ab1 files, location( directory path) and a md5sum so i know if duplicate filenames are the same file or just a result of misnaming.

managed to come up with this after copying from two different scripts

one that was used to make an inventory of  a directory of ogg songs and the other a python equivalent of md5sum check in linux.

Have fun!

#         FILE:
#        USAGE:  ./
#  DESCRIPTION:  Lists all the files of extension .ab1 with the directory and its md5sum
#  adapated from code from and
#  used md5sum code from
#      OPTIONS:  ---
#         BUGS:  will execute md5 on directory as well
#                current method to get CWD is not OS independent
#        NOTES:  ---
#       AUTHOR:  Kevin ,
#      VERSION:  1.0
#      CREATED:  11/07/2008 07:03:16 PM SGT
#     REVISION:  ---

import dircache, os, md5
counter = 0

def sumfile(fobj):
    '''Returns an md5 hash for an object with read() method.'''
    m =
    while True:
        d =
        if not d:
    return m.hexdigest()

def md5sum(fname):
    '''Returns an md5 hash for file fname, or stdin if fname is "-".'''
    if fname == '-':
        ret = sumfile(sys.stdin)
            f = file(fname, 'rb')
            return 'Failed to open file'
        ret = sumfile(f)
    return ret

def PrintFiles(indent):
    global counter
    thisDir = os.getcwd()

    for file in dircache.listdir(thisDir):
        if (file.endswith('ab1') or os.path.isdir(file)) and not file.startswith('.'):
            if file.endswith('ab1'):
                counter += 1

            currdir = os.popen("pwd") #for output of cwd currently works for linux pending upgrade to OS independent
            md5 = md5sum(file) #calls the md5sum function, md5 lib ships with Python

            ab1File.write('%s%s\t%s\t%s\n' %(indent, file, currdir.readline()[:-1], md5))

            if os.path.isdir(file):
                PrintFiles(indent + '  ')

    ab1File = open('ab1files.txt', 'w')
except IOError, e:
    print "Unable to open 'ab1files.txt' for writing: ", e
    ab1File.write('\nCurrent number of ab1 files: %d\n\n' %(counter))

Permalink 4 Comments

Python script to split a text file by even or odd numbers

June 20, 2008 at 11:50 am (opensource, software, tips) (, , , , , , )

written a short script to split a file into even or odd line numbers 🙂

## loop do something to each line of input file
## changed to write the even line numbers to a file
## and the odd line numbers to another
## note that even numbers start with line 0 (not 1!)
## usage: inputfile
##  written by kevinl @

import sys

def isodd(n):
    return bool(n%2)

input=open(sys.argv[1], 'r')
evenout=open('evenout', 'w')

for linecount in range(len(L)):
    if isodd(linecount):
    #print "line number is " + str(linecount)

Permalink Leave a Comment