-4

I need a python script counting lines in all text files in one directory and producing a general report on number of files with n number of lines.

The report should look like this:

Files with 1 line: 636
Files with 2 lines: 346
Files with 3 lines: 234
Files with 4 lines: 723
Files with 5 lines: 254
Files with 6 lines: 223
Files with 7 lines: 1464
etc.

I have found this script for counting lines in all files in a directory Python script to count num lines in all files in directory:

#!/usr/bin/env python

import csv
import copy
import os
import sys
import glob

#get current working dir, set count, and select file delimiter
os.chdir('/mydirectory')

#parses through files and saves to a dict

names={}
for fn in glob.glob('*.txt'):
    with open(fn) as f:
        names[fn]=sum(1 for line in f if line.strip() and not line.startswith('#'))    

print names

#save the dictionary with key/val pairs to a csv
with open('seriescount.csv', 'wb') as f: 
    w = csv.DictWriter(f, names.keys())
    sum(names.values())

How do we go about generating a simple report like the one above? Thanks.

Community
  • 1
  • 1
  • You have a example. So start to work and not to ask. – qvpham May 31 '16 at 09:04
  • 1
    instead of searching low quality scripts that don't solve your problem, try learning python and writing your own script. – Daniel May 31 '16 at 09:06
  • 1
    While I agree with this question being borderline, Stack Overflow's baseline is not "build an encyclopedia of searchable and public-interest articles about programming", but "Ask questions, get answers, no distractions". This question respects this philosophy and is show more research effort than most one-liners "please solve my problem for me" questions. – Jivan May 31 '16 at 09:19
  • You are quite right guys, I should try harder before asking. It's just that sometimes we need solutions to particular problems to move on with other/bigger projects. Learning takes time but we also learn by analysing answers to questions which currently are too difficult for us. I'll do just that now thanks to @Jivan. – jigitjigit2 May 31 '16 at 09:36

1 Answers1

0

Your names dict looks like that:

{
    'file1.txt': 30,
    'file2.txt': 26,
    'file3.txt': 19,
    'file4.txt': 19
}

So you'd just have to start from that and follow with:

from collections import defaultdict

lines = defaultdict(int)
for val in names.values():
    lines[val] += 1

for k, v in lines.items():
    print("Files with {} lines: {}".format(k, v))

This will print something like:

Files with 19 lines: 2
Files with 26 lines: 1
Files with 30 lines: 1
Jivan
  • 21,522
  • 15
  • 80
  • 131