Yaswanth Yaswanth - 1 year ago 75
Python Question

How to find number of lines in text file using python?

  1. i need to get a text file fro given .tst file... and find number of lines in it

  2. I'm getting 0 as output..

  3. I Need to execute program twice for getting those text files

  4. Is there a problem with my code??

name_of_file = raw_input("tst file address please:")

import re
f = open(name_of_file+".tst",'r')
data = f.read()
y = re.findall(r'Test Case:(.*?)TEST.UNIT:',data,re.DOTALL)
fb = open('tcases.txt' ,'w' )
for line in y :
z = re.findall(r'TEST.SUBPROGRAM:(.*?)TEST.NEW',data,re.DOTALL)
fc = open('tsubprgs.txt' ,'w' )
for line in z :
x = re.findall(r'TEST.UNIT:(.*?)TEST.SUBPROGRAM:',data,re.DOTALL)
fa = open('tunits.txt' ,'w' )
for line in x :

with open('tunits.txt') as foo:
lines = len(foo.readlines())
print lines

Answer Source

try this

with open(<pathtofile>) as f:
      print len(f.readlines())