简体   繁体   中英

Read large files line by line using python [on hold]

How can I read large files ( > 10GB) line by line. I know that it is not a good idea to store readlines() to a variable then I tried this code below but I'm still getting the MemoryError. I'm using python 3.7 btw.

with open('file.txt') as f:
      for line in f:
            Process(line)```

To read a file line by line,

filepath = 'file.txt'
with open(filepath) as fp:
   line = fp.readline()
   while line:
       Process(line)

You should not get an error, unless you are storing every line in memory. I suggest you process each line and then get rid of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM