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")
You need to convert the uuid
to a str
:
>>> import uuid
>>> str(uuid.uuid4()) + ".txt"
'13eb9327-f40e-4ef1-8020-1c36af1b4b70.txt'
This version doesn't include the -
in the string
fo = open(uuid.uuid4().hex + ".txt", "wb")
It's often a good idea to open the file using a context manager
with open(uuid.uuid4().hex + ".txt", "wb") as fo:
# do stuff with fo
# fo will be closed automatically
Here's an example in a loop
for item in data:
with open(uuid.uuid4().hex + ".txt", "wb") as fo:
fo.write(item)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.