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.

Revisions

  1. Headline revised this gist Sep 5, 2017. 1 changed file with 20 additions and 12 deletions.
    32 changes: 20 additions & 12 deletions LineCounter.py
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,23 @@
    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(path):
    count = 0
    file = open(path, "r")
    row = file.readlines()
    for line in row:
    count += 1
    return count
    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'):
    @@ -26,9 +30,13 @@ def is_plain_text(file):
    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)
    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: " + str(count))
    print("Total lines for all files: " + output)
  2. Headline created this gist Sep 5, 2017.
    34 changes: 34 additions & 0 deletions LineCounter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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))