12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

allsizes.sort( )print allsizes[:2]print allsizes[-2:]# Find the largest <strong>Python</strong> source file on the module import search pathimport sys, os, pprintvisited = {}allsizes = []for srcdir in sys.path:for (thisDir, subsHere, filesHere) in os.walk(srcdir):thisDir = os.path.normpath(thisDir)if thisDir.upper( ) in visited:continueelse:visited[thisDir.upper( )] = Truefor filename in filesHere:if filename.endswith('.py'):pypath = os.path.join(thisDir, filename)try:pysize = os.path.getsize(pypath)except:print 'skipping', pypathallsizes.append((pysize, pypath))allsizes.sort( )pprint.pprint(allsizes[:3])pprint.pprint(allsizes[-3:])# Sum columns in a text file separated by commasfilename = 'data.txt'sums = {}for line in open(filename):cols = line.split(',')nums = [int(col) for col in cols]for (ix, num) in enumerate(nums):sums[ix] = sums.get(ix, 0) + numfor key in sorted(sums):print key, '=', sums[key]# Similar to prior, but using lists instead of dictionaries for sumsimport sysfilename = sys.argv[1]numcols = int(sys.argv[2])totals = [0] * numcolsPart VII, Exceptions and Tools | 675

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!