第三次Python练习题

1.使用os和os.path以及函数的递归完成:给出一个路径,遍历当前路径所有的文件及文件夹打印输出所有的文件(遇到文件输出路径,遇到文件夹继续进文件夹)

python 复制代码
import os
import os.path
def list_all_file(path):
    for name in os.listdir(path):  #逐个遍历这个名称列表,每次循环拿到一个文件 / 文件夹的名称 name。
        file_path = os.path.join(path,name)  #把当前路径 path 和名称 name 拼接成完整的文件 / 文件夹路径。
        if os.path.isfile(file_path):#判断 file_path 是否是一个文件。
            print(file_path)
        elif os.path.isdir(file_path): #判断 file_path 是否是一个文件夹。
            list_all_file(file_path)
if __name__ == "__main__":
    list_all_file("D:\\Python\\code\\05.Python-io")

2.使用加密模块及IO模拟登录功能,要求使用文件模拟数据库存储用户名和密码。

python 复制代码
import hmac
datebase = {}
def encryption_admin(str):
    salt = "%%$$&&".encode("utf-8")
    return hmac.new(str.encode("utf-8"),salt,"md5").hexdigest()

datebase["username"] = encryption_admin("zhangsan")
datebase["password"] = encryption_admin("111")

username = input("请输入用户名:")
password = input("请输入密码:")
if (encryption_admin(username) == datebase["username"]) and (encryption_admin(password) == datebase["password"]):
    print("login success")
else:
    print("login failure")

3.使用面向对象编程完成学生信息录入功能,数据存储在本地文件txt中并读取学生信息并按照成绩进行排序,学生其他属性自行规划

python 复制代码
class Student:
    """学生类"""
    def __init__(self, name, age, score):
        self.name = name
        self.age = int(age)
        self.score = float(score)

    def __str__(self):
        """打印信息"""
        return f"姓名:{self.name}, 年龄:{self.age}, 成绩:{self.score}"

    def __lt__(self, other):
        return self.score < other.score

    def __eq__(self, other):
        return self.name == other.name and self.age == other.age and self.score == other.score

    def __hash__(self):
        return hash((self.name, self.age, self.score))


def save_student(name, age, score):
    """保存学生信息"""
    try:
        with open("D:\\Python\\code\\05.Python-io\\student.txt", "a", encoding="utf-8") as f:
            f.write(f"{name},{age},{score}\n")
            print("保存成功")
    except Exception as e:
        print("异常信息:", e)


def read_student():
    """读取学生信息"""
    students = set()
    try:
        with open("D:\\Python\\code\\05.Python-io\\student.txt", "r", encoding="utf-8") as f:
            for line in f:
                name, age, score = line.strip().split(",")
                students.add(Student(name, age, score))
    except Exception as e:
        print("异常信息:", e)
    return list(students)  # 将集合转换为列表


def input_student():
    """录入学生信息"""
    students = []
    while True:
        name = input("请输入学生姓名(输入q结束):")
        if name == "q":
            break
        age = input("请输入学生年龄:")
        score = input("请输入学生成绩:")
        student = Student(name, age, score)
        students.append(student)
        save_student(name, age, score)
    return students


def sort_student(students):
    """排序"""
    students.sort(reverse=True)
    return students



if __name__ == "__main__":
    # 录入学生信息
    students = input_student()
    # 保存学生信息
    for student in students:
        save_student(student.name, student.age, student.score)
    
    # 读取学生信息
    students = read_student()
    
    # 排序学生信息
    sorted_students = sort_student(students)
    
    # 打印排序后的学生信息
    print("排序后的学生信息:")
    for s in sorted_students:
        print(s)
相关推荐
码农老李12 小时前
openEuler2403服务器版 原生官方镜像和飞腾定制镜像
开发语言·php
charlie11451419112 小时前
现代Qt开发教程(新手篇)2.3——QImage、QPixmap、QIcon 图像处理基础
开发语言·图像处理·qt
范范@12 小时前
python基础-函数
开发语言·python
2301_8039346112 小时前
MySQL 字段类型选择规范指南
jvm·数据库·python
特种加菲猫13 小时前
从零开始手撕AVL树:详解插入、平衡因子更新与四种旋转
开发语言·c++
roman_日积跬步-终至千里13 小时前
如何分析复杂架构:一套真正能落地的方法
java·开发语言·架构
geovindu13 小时前
go: Semaphore Pattern
开发语言·后端·设计模式·golang·企业级信号量模式
Don.TIk13 小时前
ChaperTwo-整合 SaToken 实现 JWT 登录功能
java·开发语言
yaoxin52112314 小时前
406. Java 文件操作基础 - 字符与二进制流
java·开发语言·python
江屿风14 小时前
C++OJ题经验总结(竞赛)1
开发语言·c++·笔记·算法