Created
September 20, 2011 01:34
-
-
Save parkr/1228095 to your computer and use it in GitHub Desktop.
Python Line Count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
find . -type f | xargs cat | wc -l
or for all of them
find . -type f | xargs wc