Python program to count the total number of lines in a file

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.

algorithm

  1. ask user to input file
  2. open a file and store in a string
  3. using for loop read a file line by line
  4. increment count variable with 1 when get a new line (\n)
  5. 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

  1. whatever user will give as an input that will be store in a variable userinput1
  2. myfile will read the content of a file and store
  3. the function splitline will split the file line by line and store in a split
  4. for loop will count total line and the increament count variable
  5. 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.

 

Python program to count the total number of lines in a file
output

 

you may also like…………


 

python program to find factorial of a number

c program to calculate compound interest using function

 


 

Leave a Reply

Your email address will not be published. Required fields are marked *