Skip to content

Instantly share code, notes, and snippets.

@Morrolan
Created February 10, 2014 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Morrolan/8914905 to your computer and use it in GitHub Desktop.
Save Morrolan/8914905 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""
Count the number of lines in files recursively in a given directory.
To run direct rather than from the command line, switch the last line around.
"""
import os
import sys
def count_lines(f):
counter = 0
f = open(f, "r")
for line in f.read().split('\n'):
counter += 1
f.close()
return counter
def count_dir(dirname):
counter = 0
for f in os.listdir(dirname):
fa = os.path.join(dirname, f)
if os.path.isdir(fa):
dcount = count_dir(fa)
counter = counter + dcount
else:
fcount = count_lines(fa)
counter = counter + fcount
return counter
#print count_dir(r"C:\stuff")
print count_dir(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment