python - 给定已保存的文件名列表,如何生成固定长度的唯一字符串?
问题描述
如何生成一个新的固定长度的唯一字符串(已经使用相同的方法生成)python中保存的字符串列表?
请参阅下面的代码。我需要多次运行它(可能与完整的可能字符串集的大小一样大(比如长度为 8 个字符))。
strs= load_strings()
#strs= ['sasasafs', 'fdgfdgsa', '43f43y4f', ...]
new_str = generate_new_unique_string(strs) #how to implement this function?
strs.append(new_str)
save_strings(strs)
解决方案
使用 os 检查文件名是否存在加上使用 random 从随机名称生成一些随机文本
检查文件是否存在
import os.path
from os import path
path.exists("guru99.txt")
随机名称(例如长度为 7 个字符)
import random
name = ""
for i in range(7):
name += chr(random.randint(48,90))
print(name)
推荐阅读
- python - 比较 PCAP 数据集
- raku - 如何在使用 NativeCall 的 Raku 发行版中捆绑 C 源代码?
- firebase - StreamBuild:流,调用返回Firestore的方法 Stream在更新之前不会检索数据
- angular - 带字体模块的角度
- snowflake-cloud-data-platform - 雪花存储过程。JavaScript 内存不足错误
- scala - Scala Builder递归的对象
- laravel - Laravel,在刀片视图中使用 groupBy
- python - 无法从 PostGresSQL 表中获取行
- angular - 如何手动触发关闭 ng-bootstrap 弹出框
- python - 用于 Django3 + DRF 身份验证的 AWS Cognito