1, you enter a few, the file will give you the number of passwords generated
2. Password must include, uppercase, lowercase letters, numbers, special characters
3. Password cannot be duplicated
4, the password is randomly generated
5. Password Length 6-11
Import string, random
Upperstr= String.ascii_uppercase
Lowerstr= String.ascii_lowercase
Digitstr= String.digits
Specialstr= String.punctuation
Allstr= Upperstr+lowerstr+digitstr+specialstr#产生密码所需要的字符集
F=Open' PwdFile.txt ',' W ')
# Enter the number of times you want to generate a password:
Num=intInput"Please input The Times:")
For IInchRange (Num):
Pwdlen= Random.randint (6,11)
#print (pwdlen) # randomly generated password length
# Passwords must contain four characters: uppercase letters, lowercase letters, numbers, special characters
Pwd1= Random.choice (UPPERSTR)+ Random.choice (DIGITSTR)+ Random.choice (lowerstr) + Random.choice (specialstr)
Pwdran = random.sample (allstr,pwdLen-4) #除去4个字符外, randomly remove the remaining required characters from the character set
PWD2 = "". Join (Pwdran) # and converts the list to a string
pwd = pwd1+pwd2 # final generated random password
f.write (pwd+ \n " #将密码写入文件中
f.close ()
Build Result:
M0h[6uzo
K2m~9i (+
P9g/#<yn
o5u& ") [email protected]
E6f;hvl
D7b>[email protected] $Y 6
Summarize:
1.
Pwdran = random.sample (allstr, Pwdlen-4) #除去4个字符外, randomly remove the remaining required characters from the character set
Random.sample () generates a list that needs to be converted to a string, before it can be stitched together with the first part of the generated password.
2.
Pwd2 = "". Join (Pwdran)# and convert the list to a string
Using the Join method to convert a list to a string type
Python 3-Write a program that automatically generates a password file