如何让密码错误提示信息更具个性化?
美国、香港服务器
如何让密码错误提示信息更具个性化?
05-08 来源:
要让密码错误提示信息更具个性化,可以从以下几个角度出发,为不同用户、不同使用场景提供更贴合实际的提示内容。下面分别给出单个文件解压和批量文件解压场景下的优化示例。
单个文件解压场景
python
import zipfile
def unzip_single_password_protected_file(zip_file_path, password, extract_path, user_name=None):
try:
with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
zip_ref.setpassword(password.encode())
zip_ref.extractall(extract_path)
if user_name:
print(f"🎉 {user_name},成功解压 {zip_file_path} 到 {extract_path}")
else:
print(f"🎉 成功解压 {zip_file_path} 到 {extract_path}")
except FileNotFoundError:
if user_name:
print(f"😔 {user_name},未找到 {zip_file_path} 文件,请检查文件路径是否正确。")
else:
print(f"😔 错误:未找到 {zip_file_path} 文件,请检查文件路径是否正确。")
except zipfile.BadZipFile:
if user_name:
print(f"😕 {user_name},{zip_file_path} 不是有效的 ZIP 文件,可能文件已损坏。")
else:
print(f"😕 错误:{zip_file_path} 不是有效的 ZIP 文件,可能文件已损坏。")
except RuntimeError as e:
if "Bad password for file" in str(e):
if user_name:
print(f"❌ {user_name},你为 {zip_file_path} 输入的密码不正确哦。别着急,再仔细想想密码,或者联系文件分享者问问看。")
else:
print(f"❌ 你为 {zip_file_path} 输入的密码不正确。请再次确认密码,或者尝试联系文件提供者获取正确密码。")
else:
if user_name:
print(f"😵 {user_name},发生未知错误:{e},请检查相关环境或联系技术支持人员。")
else:
print(f"😵 发生未知错误:{e},请检查相关环境或联系技术支持人员。")
# 示例用法
zip_file = "example.zip"
password = "wrong_password"
extract_path = "extracted"
user_name = "Alice"
unzip_single_password_protected_file(zip_file, password, extract_path, user_name)
代码解释
引入用户名参数:在函数中添加 user_name 参数,根据是否提供用户名来定制提示信息。当有用户名时,在提示信息中称呼用户,让提示更具亲近感。
不同错误类型定制提示:针对不同的错误类型,如文件未找到、ZIP 文件损坏、密码错误和未知错误,在有用户名时都给出更个性化的提示,使用更亲切的语言,提供更贴心的建议。
批量文件解压场景
python
import os
import zipfile
import pathlib
def batch_unzip_password_protected_files(zip_dir, password, extract_dir, user_name=None):
zip_dir = pathlib.Path(zip_dir)
extract_dir = pathlib.Path(extract_dir)
extract_dir.mkdir(parents=True, exist_ok=True)
failed_files = []
for zip_file_path in zip_dir.rglob("*.zip"):
try:
with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
zip_ref.setpassword(password.encode())
file_extract_dir = extract_dir / zip_file_path.stem
file_extract_dir.mkdir(parents=True, exist_ok=True)
zip_ref.extractall(file_extract_dir)
if user_name:
print(f"🎉 {user_name},成功解压 {zip_file_path} 到 {file_extract_dir}")
else:
print(f"🎉 成功解压 {zip_file_path} 到 {file_extract_dir}")
except FileNotFoundError:
if user_name:
print(f"😔 {user_name},未找到 {zip_file_path} 文件,请检查文件路径是否正确。")
else:
print(f"😔 错误:未找到 {zip_file_path} 文件,请检查文件路径是否正确。")
failed_files.append(zip_file_path)
except zipfile.BadZipFile:
if user_name:
print(f"😕 {user_name},{zip_file_path} 不是有效的 ZIP 文件,可能文件已损坏。")
else:
print(f"😕 错误:{zip_file_path} 不是有效的 ZIP 文件,可能文件已损坏。")
failed_files.append(zip_file_path)
except RuntimeError as e:
if "Bad password for file" in str(e):
if user_name:
print(f"❌ {user_name},你为 {zip_file_path} 输入的密码不对呢。要不回忆下密码有没有大小写、特殊字符,或者问问文件分享者。")
else:
print(f"❌ 你为 {zip_file_path} 输入的密码不正确。请再次确认密码,或者尝试联系文件提供者获取正确密码。")
failed_files.append(zip_file_path)
else:
if user_name:
print(f"😵 {user_name},发生未知错误:{e},请检查相关环境或联系技术支持人员。")
else:
print(f"😵 发生未知错误:{e},请检查相关环境或联系技术支持人员。")
failed_files.append(zip_file_path)
if failed_files:
if user_name:
print(f"\n{user_name},以下文件解压失败,请根据上述提示进行处理:")
else:
print("\n以下文件解压失败,请根据上述提示进行处理:")
for file in failed_files:
print(f" - {file}")
else:
if user_name:
print(f"\n{user_name},所有文件均解压成功!👏")
else:
print("\n所有文件均解压成功!👏")
# 示例用法
zip_directory = "your_zip_folder"
password = "wrong_password"
extract_directory = "extracted_all"
user_name = "Bob"
batch_unzip_password_protected_files(zip_directory, password, extract_directory, user_name)
代码解释
用户名贯穿提示:在批量解压场景中同样引入 user_name 参数,在每个操作结果的提示信息中,根据是否有用户名进行个性化定制。
失败文件汇总提示:在汇总失败文件时,若有用户名,在提示信息中称呼用户,增强提示的个性化。成功提示时也同理,让用户感受到更贴心的反馈。
通过以上方式,可以让密码错误提示信息更具个性化,提升用户体验。
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快