Skip to main content
edited title
Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

Python getting Getting a hash string for a very large file

edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
deleted 2 characters in body
Source Link
Quill
  • 12.1k
  • 5
  • 41
  • 94

After reading about large files and memory problems, I'm suspecting that my code below may be inefficient because it reads the entire files into memory before applying the hash algorithm. IsIs there a better way??

chunk_size = 1024 hasher = hashlib.md5() while True: try: data = f.read(chunk_size) except IOError, e: log.error('error hashing %s on Agent %s' % (path, agent.name)) return {'error': '%s' % e} if not data: break hasher.update(data) hash_string = hasher.hexdigest() 

After reading about large files and memory problems, I'm suspecting that my code below may be inefficient because it reads the entire files into memory before applying the hash algorithm. Is there a better way??

chunk_size = 1024 hasher = hashlib.md5() while True: try: data = f.read(chunk_size) except IOError, e: log.error('error hashing %s on Agent %s' % (path, agent.name)) return {'error': '%s' % e} if not data: break hasher.update(data) hash_string = hasher.hexdigest() 

After reading about large files and memory problems, I'm suspecting that my code below may be inefficient because it reads the entire files into memory before applying the hash algorithm. Is there a better way?

chunk_size = 1024 hasher = hashlib.md5() while True: try: data = f.read(chunk_size) except IOError, e: log.error('error hashing %s on Agent %s' % (path, agent.name)) return {'error': '%s' % e} if not data: break hasher.update(data) hash_string = hasher.hexdigest() 
Source Link
MFB
  • 337
  • 4
  • 12
Loading