在 Python 中,生成一些随机文本以添加到我保存到服务器的文件(名称)之前的好方法或最佳方法是什么,以确保它不会被覆盖。谢谢!
最佳答案
您可以使用 UUID module用于生成随机字符串:
import uuid
filename = str(uuid.uuid4())
这是一个有效的选择,因为 UUID生成器极不可能产生重复的标识符(在这种情况下是文件名):
Only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs.
关于python - 在 Python 中生成随机文件名的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10501247/