Answers for "how many line in file python"

10

python 3 text file leng

f = open("filename", "r") #Load file in any mode that's able to read, ie r, r+, w+ etc

#to get length
len(f.readlines())

#To iterate over each line
for line in f.readlines(): #file.readlines(), splits the file into a list, where each element is a seperate line
  print(line)
Posted by: Guest on March-02-2020
2

write number of lines in file python

fname = "test.txt"
count = 0
with open(fname, 'r') as f:
    for line in f:
        count += 1
print("Total number of lines is:", count)
Posted by: Guest on January-29-2021

Code answers related to "how many line in file python"

Python Answers by Framework

Browse Popular Code Answers by Language