How to open a file and read line by line in python

 

1st method: using readline() function

f = open("file_name.txt")
# for line in f:
#     print(line)
content = f.readline() #reads line 1
print(content)
content = f.readline() #reads line 2
print(content)
content = f.readline() #reads line 3
print(content)
content = f.readline() #reads line 4
print(content)
f.close()



2nd method: using for loop

f = open("file_name.txt")
for line in f:
    print(line)
f.close()




Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

What is the difference between iostream and iostream.h in cpp?

The Basic Structure of a Full-Stack Web App