Skip to content

Instantly share code, notes, and snippets.

@parkr
Created September 20, 2011 01:34
Show Gist options
  • Save parkr/1228095 to your computer and use it in GitHub Desktop.
Save parkr/1228095 to your computer and use it in GitHub Desktop.
Python Line Count
#! /usr/bin/env python
# By Parker Moore, http://www.parkermoore.de/
# Referenced the following URLs and compiled:
# http://lookherefirst.wordpress.com/2007/12/03/check-if-an-entry-is-a-file-or-directory-in-python/
# http://stackoverflow.com/questions/845058/how-to-get-line-count-cheaply-in-python
import sys, os
def file_len(fname):
i = -1
with open(fname) as f:
for i, l in enumerate(f):
pass
return i+1
def count(fname):
_count = 0
dirList = os.listdir(fname)
for d in dirList:
if os.path.isdir(fname+"/"+d) == True:
_count += count(str(fname+"/"+d))
else:
filelen = file_len(fname+"/"+d)
print "%s: %d" % (fname+"/"+d, filelen)
_count += filelen
return _count
total = count(sys.argv[1])
print "Total:", total
@leifwalsh
Copy link

find . -type f | xargs cat | wc -l

or for all of them

find . -type f | xargs wc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment