Skip to content

Instantly share code, notes, and snippets.

@ettorerizza
Created September 28, 2017 18:15
Show Gist options
  • Save ettorerizza/bd7f4cc12e22b847c5e9651ba2be2844 to your computer and use it in GitHub Desktop.
Save ettorerizza/bd7f4cc12e22b847c5e9651ba2be2844 to your computer and use it in GitHub Desktop.
Select all txt files in a folder, count lines and write the result in csv
import csv
import copy
import os
import sys
import glob
os.chdir(r"FOLDER_PATH")
names={}
for fn in glob.glob('*.txt'):
with open(fn, encoding="utf8") as f:
names[fn]=sum(1 for line in f if line.strip())
print(names)
with open('countlines.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in names.items():
writer.writerow([key, value])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment