Skip to content

Instantly share code, notes, and snippets.

@Headline
Last active September 5, 2017 08:42
Show Gist options
  • Save Headline/b56537696fa128fcafb3c061e79c181e to your computer and use it in GitHub Desktop.
Save Headline/b56537696fa128fcafb3c061e79c181e to your computer and use it in GitHub Desktop.
A python script to count the amount of lines of text for all of it's adjacent files & files in sub directories.
import os
import os.path
import mimetypes
import locale
def get_clean_path(array):
somestr = ""
for x in range(0, len(array) - 1):
somestr += array[x] + "\\"
return somestr
def get_line_count(fname):
if not os.path.isfile(fname):
return 0
with open(fname, errors="ignore") as f:
i = 0
for i, l in enumerate(f):
pass
return i + 1
def is_plain_text(file):
if (mimetypes.guess_type(file)[0] == 'text/plain'):
return True
else:
return False
path = get_clean_path(os.path.realpath(__file__).split("\\"))
count = 0
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
current = dirpath + "\\" + filename
if is_plain_text(current):
line_count = get_line_count(current)
count += line_count
print("Adding: " + current + "(" + str(line_count) + ")")
locale.setlocale(locale.LC_ALL, 'english')
output = locale.format('%d', count, grouping=True) # returns '4,294,967,296'
print("Total lines for all files: " + output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment