How to count number of line in a text file using python

 Hello, everyone I am Rohit at road2geeks today we will be discussing how to make a program in python that takes a text file and then tell us how many lines are there in the .txt file today's blog gonna be much interesting for programming lovers and in this video, I will discuss the logic of the code that I have made. so coming towards today's topic I will recommend you to learn python basics to have the best experience of this video. if you don't have learned python basics you can skip this blog and go to the end part to get the output and source code of this python file for free! 

How to count number of line in a text file using python

 so for your information, I am on python 3.9.6 that I have installed earlier if you don't know how to install this python you can check my blog by clicking here. and I am on vs code today the Microsoft most successful IDE  so I have written a couple of codes earlier and in this blog, I will be discussing the program in this code coming to the first line according to python control. as you know that the python doesn't read the functions first so it will initially go to 

line number 11 -

path_of_file=input("enter the path of file \
:\n(example:\\folder\\file.extension or \
    example:/folder/file.extension)\n")

which says here the control creates a variable named path_of_file which will basically take input of the path of the text file then control goes to 

line number 13

file_2_open=open(rf"{path_of_file}""r")

 where control makes another variable named file_2_open which will open the file at the location  the user has provided in line number 11 then control goes to 

line 14

reading_lines=file_2_open.readlines()

 where control creates another variable named reading_lines which will read the file with all lines including escape sequences in a list format so in 

line number 15

str_reading_lines=str(reading_lines)

 I have made another variable namedstr_reading_lines which will basically store the string value of reading_lines then control goes to 

line 16

print(f"there are {countline(str_reading_lines)} \
lines in the text file")

 which tells the control to print there are countline(str_reading_lines) lines in the text file so here control will go to search for a python function named countlines to get the output and I have defined this function in 

line 1 

def countline(lines):

so it takes the value of str_reading_lines as lines in this function and I have defined a docstring  in 

line 2 

'''program to count new lines and takes sting'''

then python goes to 

line 3 

count=1#counting no of lines

which says make a variable named count and its value should be one the purpose of using count is to count lines in the string that I have provided 

line 4 

determinebackslash=""

says to generate a blank string variable named diterminebackslash its basically used to identify \ 

line5 

for line in lines:

says to create a loop it will run till the lines are finished with line step 

line6 

for letter in line:

says to create a nested loop which will run till the line ends and with letter steps and update the value of the variable named letter 

line 7 and 8 

if determinebackslash=="\\" and  letter=="n":
count+=1

says if the variable named diterminebackslash gets the value \\ (by the way purpose3 of using \\ is due to escape sequence for a single \ ) if the variable named diterminebackslash gets the value \\ and letter named variable gets the value "n" then add 1 to variable named count

line 9

determinebackslash=letter

 says to update the value of the variable named diterminebackslash  as the value in the variable named letter  

line 10

return count

 says to return the value count to the needy here the needy is line 16 so it returns the value there and goes to 

line 17

file_2_open.close()

 which says that close file which is stored in the variable named file_2_open now its result time so, at this point, I will run the program and now the terminal asks me to enter the path of the file so I have created a text file with 5 lines in this folder simply take the path of this file and paste it on the terminal after pasting it terminal says there are 5 lines in the text file. and then successfully ends the program.  now I will edit the text file and increase the number of lines to 7 by pressing enter twice so now save this file and open vs code and run this file again in vs code so it ends successfully with the correct output 

source code-

source code


Video information-

and if this blog seems to be informative please give a comment and follow this Road2Geeks BLOG to get more such content that is all for today's blog, meet you next time. 

Post a Comment

0 Comments