I need to write a program that count the len of the lines in file and return the number of lines >= to the len size the user asked.
The problem is that len() count (\n) and I can’t assume after the last line there is (\n).
How can I tell len() to not count (\n) in the end of every line?
def count_long_lines(filename, size):
f=open(filename,'r')
count_line=0
for line in f:
if len(line)-1>=size:
count_line +=1
print count_line
f.close()
Use
.rstrip('\n')to remove any newlines from the line: