Skip to content

Instantly share code, notes, and snippets.

@Headline
Last active September 5, 2017 08:42
Show Gist options
  • Select an option

  • Save Headline/b56537696fa128fcafb3c061e79c181e to your computer and use it in GitHub Desktop.

Select an option

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 mimetypes
def get_clean_path(array):
somestr = ""
for x in range(0, len(array) - 1):
somestr += array[x] + "\\"
return somestr
def get_line_count(path):
count = 0
file = open(path, "r")
row = file.readlines()
for line in row:
count += 1
return count
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)):
print("Adding: " + current)
count += get_line_count(current)
print("Total lines for all files: " + str(count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment