Created
September 28, 2017 18:15
-
-
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
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
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