0

I want to add file identifiers to a script I am creating that are short and unique. The identifier needs to be related to the input arguments given to the script from the CLI, so that the same identifier gets generated from the same arguments always.

I thought of hashing them with an MD5 hash that gets as input all the arguments provided by the user in the CLI:

import sys
import hashlib

file_id = hashlib.md5(str(sys.argv).encode("utf-8"))
print(file_id)

This only gives me an object description & memory location:

<md5 HASH object @ 0x0000324294...>

If I could get the actual hash I could extract the first 20 characters or so and it would be good enough for my use case.

0

1 Answer 1

3

You can use the hexdigest method like so:

import sys
import hashlib

my_hash = hashlib.md5()
my_hash.update(str(sys.argv).encode("utf-8"))
print(my_hash.hexdigest())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.