python - 在 Python 中生成随机文件名的最佳方法

标签 python random hash

在 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/

相关文章:

c++ - 散列函数中质数的使用分析

python - python内部类的目的是什么?

python - 如何使syncdb显示完整的堆栈跟踪

python - 如何将 Boost.Python 中的 map_indexing_suite 与自定义非 std 对象一起使用?

mysql - 从给定的SQL表中提供随机元素的服务是否可行?

java - 每次生成不同的密码哈希

arrays - 散列的散列 : How to get the number of occurrences of a key?

python - Django - 从媒体目录提供文件

VBA Excel "error 13: type mismatch"

javascript - 在 URL 中实现 JavaScript 和文本