Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use uuid.uuid4() to create new file

Tags:

python

How do you concatenate the uuid.uuid4() value with a literal when creating a file? The below isn't correct but should illustrate what I'm attempting to do...

fo = open(uuid.uuid4() + ".txt", "wb")
like image 303
c12 Avatar asked Nov 14 '12 04:11

c12


People also ask

What is UUID uuid4 in Python?

UUID, Universal Unique Identifier, is a python library which helps in generating random objects of 128 bits as ids. It provides the uniqueness as it generates ids on the basis of time, Computer hardware (MAC etc.).

What is UUID uuid4?

uuid4() creates a random UUID. New in version 3.7. The UUID was generated by the platform in a multiprocessing-safe way. The UUID was not generated in a multiprocessing-safe way.

How do you create a unique UUID in Python?

The uuid module provides immutable UUID objects (the UUID class) and the functions uuid1() , uuid3() , uuid4() , uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified in RFC 4122. If all you want is a unique ID, you should probably call uuid1() or uuid4() .

How do I get the UUID in Python?

UUID 1 to Generate a unique ID using MAC AddressThe uuid. uuid1() function is used to generate a UUID from the host ID, sequence number, and the current time. It uses the MAC address of a host as a source of uniqueness. The node and clock_seq are optional arguments.


1 Answers

You need to convert the uuid to a str:

>>> import uuid
>>> str(uuid.uuid4()) + ".txt"
'13eb9327-f40e-4ef1-8020-1c36af1b4b70.txt'
like image 64
Jonathon Reinhart Avatar answered Sep 28 '22 06:09

Jonathon Reinhart