效果:直接运行即可
![](https://img-blog.csdnimg.cn/direct/3d46ee869e914293af2ce19b8525518f.png)
import random
import string
# 定义包含小写字母和数字的字符集
chars = string.ascii_lowercase + string.digits
# 要生成的密码总数
total_passwords = 100000000 # 1亿
# 每批次生成的密码数量
batch_size = 1000000 # 例如,每次生成100万个密码
# 用于存储已生成密码的文件名
generated_passwords_file = 'generated_passwords03.txt'
# 打开文件以追加写入密码
with open(generated_passwords_file, 'a') as f:
# 当前已生成的密码数量
current_count = 0
while current_count < total_passwords:
# 生成当前批次的密码集合
batch_passwords = set()
while len(batch_passwords) < batch_size:
password = ''.join(random.choice(chars) for _ in range(8))
batch_passwords.add(password)
# 将当前批次的密码写入文件
for password in batch_passwords:
f.write(password + '\n')
# 更新已生成的密码数量
current_count += len(batch_passwords)
print(f"已生成 {current_count} 个密码")
print(f"共生成了 {total_passwords} 个密码,并已写入 {generated_passwords_file} 文件。")