python read file line by line

34

python read file line by line -

with open("file.txt") as file_in:
    lines = []
    for line in file_in:
        lines.append(line)

read a file line by line into a list -

with open(fname) as f:
    content = f.read().splitlines()

Read text file line by line using the readline() function -

# Program to read all the lines in a file using readline() function
file = open("python.txt", "r")
while True:
	content=file.readline()
	if not content:
		break
	print(content)
file.close()


Comments

Submit
0 Comments