python 3 - 寫一個自動產生密碼檔案的程式

來源:互聯網
上載者:User

標籤:程式   必須   使用   int   als   取出   sci   2.0   input   

1、你輸入幾,檔案裡面就給你產生多少條密碼
2、密碼必須包括,大寫字母、小寫字母、數字、特殊字元
3、密碼不能重複
4、密碼都是隨機產生的
5、密碼長度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‘)
# 輸入要產生密碼的次數:
Num = int(input("please input the times:"))
for i in range(Num):
    pwdLen = random.randint(6,11)
    #print(pwdLen) # 隨機產生密碼的長度
    # 密碼必須包含的四個字元:大寫字母,小寫字母,數字,特殊字元
   
pwd1 = random.choice(upperStr) + random.choice(digitStr) + random.choice(lowerStr) + random.choice(specialStr)
    pwdRan = random.sample(allStr,pwdLen-4) #除去4個字元外,隨機從字元集中取出剩下所需要的字元
   
pwd2 = "".join(pwdRan)#  並將該List轉化為字串
   
pwd = pwd1+pwd2 # 最終產生的隨機密碼
   
f.write(pwd+‘\n‘) #將密碼寫入檔案中
f.close()

 

產生結果:

M0h[6UZO
K2m~9I(+
P9g/#<Yn
O5u&")[email protected]
E6f;hVL
D7b>[email protected]$Y6

 

總結:

1. 

pwdRan = random.sample(allStr,pwdLen-4) #除去4個字元外,隨機從字元集中取出剩下所需要的字元

random.sample()  產生的是一個List,需要將該List轉換為字串,然後才可以和第一部分產生的密碼拼接. 

2. 

pwd2 = "".join(pwdRan)#  並將該List轉化為字串

使用join方法將list 轉化為string類型

 

python 3 - 寫一個自動產生密碼檔案的程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.