#!/usr/bin/python2.5
"""Simple character counter for Python source code
- Removes all comments that start with a hash (#)
- Removes all whitespace at the beginning and end of the line
- Counts a newline as a single character
"""
def SizeOf(filename):
try:
source_file = open(filename)
source_code = [line.strip(' \t\n\r') for line in source_file.readlines()]
source_code = [line for line in source_code
if not line.startswith('#') and line != '']
return len(' '.join(source_code))
except IOError:
print 'Couldn\'t read the file. Check presence / privileges and try again.'
except:
# Something funky happened if we end up here
print 'Whoa there! Are you sure this sort of thing is even legal!?'
sys.exit(1)
if __name__ == '__main__':
import sys
if not sys.argv[1]:
print 'The first argument should be a filename to calculate the size of'
sys.exit(1)
print '\nThe significant charcount of this code is: %d' % SizeOf(sys.argv[1])