Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python How to Create UUID Based on File Contents

I was looking at the documentation and in the example section, I don't see how to create a UUID based on File Contents. Google did not help me either.

I've tried this:

>>> import uuid
>>> data = open('/media/emmc/DCIM/100ABC06/00059.JPG','rb')
>>> contents = data.read()
>>> len(contents)
9155
>>> uuid = uuid.UUID(contents)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/uuid.py", line 134, in __init__
ValueError: badly formed hexadecimal UUID string

Also this:

>>> uuid = uuid.UUID(str(contents))
>>> uuid = uuid.UUID(contents.decode('ascii'))
>>> uuid = uuid.UUID(contents.decode('utf8'))

Please help me understand how to generate a UUID based on File contents in Python 2.7.

like image 240
PhilBot Avatar asked Mar 19 '26 13:03

PhilBot


1 Answers

If you want to create a hash of a file content, you probably don't need UUID. Instead, you should use hashlib and MD5, SHA-1, SHA-256 or any other supported algorithm to create a fingerprint of your file.

like image 105
Antwane Avatar answered Mar 22 '26 01:03

Antwane