#!/usr/bin/python3 # -*- coding: utf-8 -*- import random,string src = string.ascii_letters + string.digits data = random.sample(src,0) data.extend(random.sample(string.digits, 3))#包含3个数字 data.extend(random.sample(string.ascii_lowercase, 3))#包含3个小写字母 data.extend(random.sample(string.ascii_uppercase, 2))#包含2个大写字母 random.shuffle(data) passwd = ''.join(data) print(passwd)
高级版
#!/usr/bin/python3 # -*- coding: utf-8 -*- import pyperclip import random import string import time while True: pw_type = input(" 生成的密码类型 " "【a 包含数字】、" "【b 包含数字、小写字母】、" "【c 包含数字、大小写字母】 " "【请输入 a 或 b 或 c】:") while True: if pw_type != "a" and pw_type != "b" and pw_type != "c": pw_type = input("【请输入 a 或 b 或 c】:") else: break print("") pw_len = input(" 生成的密码长度 " "【请输入4-10】:") while True: while True: if not pw_len.isdigit(): pw_len = input("【请输入4-10】:") else: pw_len = int(pw_len) break if 11 > pw_len > 3: break else: pw_len = input("【请输入4-10】:") if pw_type == "a": src = string.digits data = random.sample(src, pw_len) elif pw_type == "b": src = string.ascii_lowercase + string.digits data = random.sample(src, pw_len - 4) data.extend(random.sample(string.digits, 2)) data.extend(random.sample(string.ascii_lowercase, 2)) elif pw_type == "c": src = string.ascii_letters + string.digits data = random.sample(src, pw_len - 4) data.extend(random.sample(string.digits, 2)) data.extend(random.sample(string.ascii_lowercase, 1)) data.extend(random.sample(string.ascii_uppercase, 1)) random.shuffle(data) passwd = ''.join(data) print(" ", "密码:", passwd, " ") pyperclip.copy(passwd) print("已复制到剪贴板 " "")
在用pyinstaller打包成exe文件
pip install pyinstaller
pyinstaller.exe -F -c .passwd.py