Python program to count the total number of lines in a file
in this tutorial, we will learn Python program to count the total number of lines in a file, the program will ask user to input the name of a file location and the program will give a total number of line in a file.
- ask user to input file
- open a file and store in a string
- using for loop read a file line by line
- increment count variable with 1 when get a new line (\n)
- print count
''' store a file in variable''' userinput1 = input ("Enter file name:") ''' read a file ''' myfile1 = open(userinput1).read() ''' split a line''' split=myfile1.splitlines() ''' initialize count with zero''' count=0 ''' count a line in a file''' for i in split: count+=1 '''print total line''' print(total line, count)
explanation of a program
- whatever user will give as an input that will be store in a variable userinput1
- myfile will read the content of a file and store
- the function splitline will split the file line by line and store in a split
- for loop will count total line and the increament count variable
- total number of lines will print as an output
this is a demo to find the total number of line in a text file hy how are you do you like my blog dont forget to write a comment.
you may also like…………