in python , suppose i have file data.txt . which has 6 lines of data . I want to calculate the no of lines which i am planning to do by going through each character and finding out the number of ‘\n’ in the file . How to take one character input from the file ? Readline takes the whole line .
Share
I think the method you’re looking for is
readlines, as inThis will give you a list of each of the lines in the file. To find out how many lines, you can just do:
And then access it using indexes, like
lines[3]orlines[-1]as you would any normal Python list.