Python 3-Write a program that automatically generates a password file

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.