我想为什么当我尝试将令牌保存到保存时总是保存第一个令牌
#!/usr/bin/python
import random
import string
count = 1
while count <= 5:
t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
key = str(''.join(random.sample(t,33)))
with open('keys','w') as keys:
keys.write(key)
count += 1
答案 0 :(得分:0)
#!/usr/bin/python
import random
import string
count = 1
with open('keys','w') as keys:
while count <= 5:
t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
key = str(''.join(random.sample(t,33)))
keys.write(key)
count += 1
答案 1 :(得分:0)
您需要附加文件而不是每次都写入文件,即在open
中使用'a'代替-w作为-
#!/usr/bin/python
import random
import string
count = 1
while count <= 5:
t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
key = str(''.join(random.sample(t,33)))
with open('keys','a') as keys:
keys.write(key)
count += 1
您可能还需要在每个键之后添加换行符。
始终保存第一个
不,您的代码不会保存第一个密钥,而是保存最后一个生成的密钥。