fname = "test.txt"
count = 0
with open(fname, 'r') as f:
for line in f:
count += 1
print("Total number of lines is:", count)
num_lines = sum(1 for line in open('myfile.txt'))
num_lines = sum(1 for line in open('myfile.txt'))
# Notice: Problem with this solution is that it won't
# count empty line if it is at end of file.
# You can do it by iterating through a simple for loop
fhand = open('path and the file name')
count = 0
for line in fhand:
count += 1
print('The number of lines in the file is:', count)