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
Post a Comment
Write something to CodeWithAbdur!