Python 基础 IO 实战|递归遍历文件夹、文件存储模拟登录系统 第一题递归遍历目录使用os和os.path以及函数的递归完成:给出一个路径遍历当前路径所有的文件及文件夹打印输出所有的文件遇到文件输出路径遇到文件夹继续进文件夹)import osdef print_all_files(dir_path):递归遍历目录打印所有文件的完整路径:param dir_path: 要遍历的目录路径# 先判断路径是否合法存在if not os.path.exists(dir_path):print(f错误路径 {dir_path} 不存在)returnif not os.path.isdir(dir_path):print(f错误{dir_path} 不是一个有效目录)return# 遍历当前目录下的所有条目文件文件夹for item_name in os.listdir(dir_path):# 拼接成完整路径必须用os.path.join兼容不同操作系统full_path os.path.join(dir_path, item_name)# 判断如果是文件直接打印路径if os.path.isfile(full_path):print(full_path)# 判断如果是目录递归调用自身进入子目录继续遍历elif os.path.isdir(full_path):print_all_files(full_path)if __name__ __main__:target input(请输入要遍历的目录路径)print_all_files(target)———————————————————————————————————————————第二题加密 文件 IO 模拟登录系统2.使用加密模块及I0模拟登录功能,要求使用文件模拟数据库存储用户名和密码。import hashlib# 密码加密def enc(pwd):return hashlib.md5(pwd.encode()).hexdigest()# 注册写入文件def register(user, pwd):with open(user.txt, a) as f:f.write(f{user}:{enc(pwd)}\n)# 登录读取校验def login(user, pwd):pwd_md5 enc(pwd)with open(user.txt, r) as f:for line in f:u, p line.strip().split(:)if u user and p pwd_md5:print(登录成功)returnprint(登录失败)register(test, 123456)login(test, 123456)login(test1, 123456)