Created
February 10, 2014 12:21
-
-
Save Morrolan/8914905 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/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