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 :
fb.write(line)
z = re.findall(r'TEST.SUBPROGRAM:(.*?)TEST.NEW',data,re.DOTALL)
fc = open('tsubprgs.txt' ,'w' )
for line in z :
fc.write(line)
x = re.findall(r'TEST.UNIT:(.*?)TEST.SUBPROGRAM:',data,re.DOTALL)
fa = open('tunits.txt' ,'w' )
for line in x :
fa.write(line)
with open('tunits.txt') as foo:
lines = len(foo.readlines())
print lines
答案 0 :(得分:8)
试试这个
with open(<pathtofile>) as f:
print len(f.readlines())
答案 1 :(得分:1)
在您的示例中,re.findall()
返回一个列表,您可以在不重新打开和计算结果文件的情况下获取匹配数,例如:
x = re.findall(r'TEST.UNIT:(.*?)TEST.SUBPROGRAM:',data,re.DOTALL)
num_tunits = len(x)
请参阅other answers了解文件行计数的想法。
答案 2 :(得分:0)
myfile = "names.txt"
num_names = 0
with open(myfile, 'r') as f:
for line in f:
names = line.split()
num_names += len(names)
print (num_names)