【发布时间】:2018-04-18 22:13:20
【问题描述】:
我正在尝试创建密码生成器。该程序应该生成一个随机密码列表,然后将它们写入.txt 文件。使用下面的代码,程序只写最后生成的密码,而不是全部。
#!/usr/bin/python3
import random
#The Characters List
Characters = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=[];'\,./ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
#Password Generator
#Get the Input form the User
Length = input("Password Length > ")
Length = int(Length)
#Get the Input form the User
PasswordNumber = input("Number of Passwords > ")
PasswordNumber = int(PasswordNumber)
for p in range(PasswordNumber):
Password = ""
for i in range(Length):
Password += random.choice(Characters)
print(Password)
f= open("PasswordList.txt","w+")
f.write(Password)
f.close()
我的结果:
Password Length > 10
Number of Passwords > 5
LgoQ$i%e_O
![i/NxsqQr
n-ydWA/9.5
ksI,jg]#8q
![xrU#=2@#
但是当我打开“PasswordList.txt”时,里面的文字只有:![xrU#=2@#,它生成的最后一个密码。
我还希望密码与终端上看到的格式相同:
LgoQ$i%e_O
![i/NxsqQr
n-ydWA/9.5
ksI,jg]#8q
![xrU#=2@#
不是这样的:
LgoQ$i%e_O![i/NxsqQrn-ydWA/9.5ksI,jg]#8q![xrU#=2@#
【问题讨论】:
-
在 for 循环中添加文件打开、写入和关闭语句以使其工作
标签: python python-3.x text passwords