Skip to content Skip to sidebar Skip to footer

Python Script To Count Num Lines In All Files In Directory

So I'm new to python and I'm trying to write a script that iterates through all .txt files in a directory, counts the number of lines in each one (with exception to lines that are

Solution 1:

You can try something along these lines:

os.chdir(ur_directory)
names={}
for fn in glob.glob('*.txt'):
    withopen(fn) as f:
        names[fn]=sum(1for line in f if line.strip() andnot line.startswith('#'))    

print names     

That will print a dictionary similar to:

{'test_text.txt':20, 'f1.txt':3, 'lines.txt':101, 'foo.txt':6, 'dat.txt':6, 'hello.txt':1, 'f2.txt':4, 'neglob.txt':8, 'bar.txt':6, 'test_reg.txt':6, 'mission_sp.txt':71, 'test_nums.txt':8, 'test.txt':7, '2591.txt':8303} 

And you can use that Python dict in csv.DictWriter.

If you want the sum of those, just do:

sum(names.values())

Solution 2:

I think you should make two changes to your script:

  • Use glob.glob() to get the list of files matching your desired suffix
  • Use for line in file_obj to iterate through the lines

Other problem:

  • The indentation is wrong on your last few lines

Solution 3:

You could count your lines in your files with this 1-liner:

line_nums = sum(1for line inopen(f) if line.strip() and line[0] != '#')

that would shorten your code segment to

for f in txt_files:
    count += sum(1 for line in open(os.path.join(d,f)) 
                 if line[0] != '#' and line.strip())

Solution 4:

I looks like you want to use a dictionary to keep track of the counts. You could create one a the top like this counts = {}

Then (once you fix your tests) you can update it for each non-comment line:

series_dict = {}
txt_files = [i for i in os.listdir(d) if os.path.splitext(i)[1] == ext]
#selects all files with .txt extensionfor f in txt_files:
    counts[f] = 0# create an entry in the dictionary to keep track of one file's lines withopen(os.path.join(d,f)) as file_obj:
        series_dict[f] = file_obj.read()

        if line.startswith("#"):   #Exclude commented linescontinueelif line.strip():                #Exclude blank lines
            counts(f) += 1

Post a Comment for "Python Script To Count Num Lines In All Files In Directory"