Python Forum
Memory Error While generating cheksum no
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memory Error While generating cheksum no
#1
Hi Team,

I am trying to create checksum no , getting error memory error.
for syntax====> lines = f.read()

file size of CSV is 60gb , how to avoid memory error while creating checksum.


code I am using.
import hashlib
import os

def Create_chk_sum(fpath,tbl):
	fnameCSV = f'{tbl}.csv'
	fn = os.path.join(fpath,fnameCSV)
	fn_dn = fn
	m = hashlib.md5()
	with open(fn,'rb') as f:
		lines = f.read()
		m.update(lines)
		chk_sum = m.hexdigest()
		fn = os.path.join(fpath,f'ChkSum_No_{tbl}.csv')
		with open(fn,'w') as data:
			data.write(chk_sum)

if __name__ == "__main__":   
    folderpath = "C:\\Users\\mg\\OneDrive\\Desktop\\C\\test_data"
    tbl = "SQL_Table1"
    Create_chk_sum(folderpath, tbl)
Reply
#2
f.read() will try to read the entire file. You tell us the file is 60GB. If your computer does not have 60GB of memory you will get a memory error.
Solution: read the file in chunks of let's say 10240 bytes (f.read(10240)) and repeat until you read a chunk of size 0.
Reply
#3
Hi ibreeden,

superb ! Thanks for your help. it worked.



Thanks
mg
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Understanding and debugging memory error crashes with python3.10.10 Arkaik 5 2,113 Apr-18-2023, 03:22 AM
Last Post: Larz60+
  Gui slot machine-out of memory error steve_shambles 12 5,149 May-18-2020, 11:31 PM
Last Post: deanhystad
  Go around memory error Pytonormen 1 2,087 Oct-19-2019, 04:31 PM
Last Post: Gribouillis
  memory error using permutation list of 11 elements kikidog 1 3,909 Sep-10-2019, 08:22 PM
Last Post: ichabod801
  Fix Memory Error while installing a library for Qgis alexastorga 0 2,590 Apr-13-2018, 04:54 PM
Last Post: alexastorga
  Dictionaries and memory error. jarrod0987 5 5,526 Feb-23-2018, 12:15 PM
Last Post: buran

Forum Jump:

User Panel Messages

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