To read large files one line at a time

Asked 2 years ago, Updated 2 years ago, 98 views

Hello, I'm NEW B who just started Python I want to read a big file one by one and process it I only know how to read the whole thing and then read it one line at a time

I'd like to know if there's another way.

for each_line in fileinput.input(input_file):
    do_something(each_line)

    for each_line_again in fileinput.input(input_file):
        do_something(each_line_again)

file-io python

2022-09-22 12:33

1 Answers

The most pythonic way to read files one line at a time is

with open(...) as f:
    for line in f:
        //Process here

This is.

Buffered-IO automatically manages memory, so you can run large files without any problems


2022-09-22 12:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.