Python Forum
Working with large volume of data (RAM is not enough)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with large volume of data (RAM is not enough)
#5
Reading a file line by line (if text) will only use enough memory for the actual record (line):
Note: none of this code has been tested.
Instead of:
with open('Myfile.txt') as fp:
    buffer = fp.readlines()
for line in buffer:
   ... do stuff
which will read in the entire file,

use to read record by record:
with open('Myfile.txt') as fp:
    for line in fp:
       ... do stuff
only one record at a time.

This however doesn't help if it's a binary file. In this instance, you can read in chunks:
In which case open file as 'rb' and read chunk by chunk (keep in mind last chunk can be any size up to chunksize, including 0):
fp.read(chunksize)
Reply


Messages In This Thread
RE: Working with large volume of data (RAM is not enough) - by Larz60+ - Oct-21-2018, 03:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Calculate the volume of piles of materials using aerial drone images WtonS 0 583 Oct-29-2024, 09:21 PM
Last Post: WtonS
  mute spotify by the mixer of volume garze23 0 867 Feb-27-2024, 05:42 PM
Last Post: garze23
  Looping Through Large Data Sets JoeDainton123 10 6,030 Oct-18-2020, 02:58 PM
Last Post: buran
  Extract data from large string pzig98 1 2,692 Jul-20-2020, 12:39 AM
Last Post: Larz60+
  Create a 3D volume with some properties. Rosendo 0 1,921 Jul-18-2020, 08:20 PM
Last Post: Rosendo
  Moving large amount of data between MySql and Sql Server using Python ste80adr 4 5,150 Apr-24-2020, 01:24 PM
Last Post: Jeff900
  alternative to nested loops for large data set JonnyEnglish 2 3,539 Feb-19-2020, 11:26 PM
Last Post: JonnyEnglish
  Working with CSV data and iterating through a file skoobi 1 2,067 Aug-13-2019, 03:28 PM
Last Post: Gribouillis
  Windows Volume Control using python Arun 1 5,560 May-17-2019, 02:50 PM
Last Post: Larz60+
  how to load large data into dataframe. sandy 0 3,120 Feb-01-2019, 06:19 PM
Last Post: sandy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020