
python
# 1
import os
path = "./05python-io"
def find_al_files(p):
for f in os.listdir(p):
fp = os.path.join(p, f)
if os.path.isfile(fp):
print(fp)
else:
find_al_files(fp)
find_al_files(path)
测试结果:


python
# 2
username = input("注册用户名:")
password = input("注册密码:")
with open("user.txt", "a") as f:
f.write(f"{username},{password}\n")
login_username = input("登录用户名:")
login_password = input("登录密码:")
success = False
with open("user.txt", "r") as f:
for line in f:
u, p = line.strip().split(",")
if u == login_username and p == login_password:
success = True
print("登录成功" if success else "失败")
测试结果:


python
# 3
# 录入3个学生
s1 = input("输入学号、姓名、年龄、成绩(用逗号分隔):").split(",")
s2 = input("输入第二个学生:").split(",")
s3 = input("输入第三个学生:").split(",")
# 转为列表(成绩用于排序)
students = [
[s1[0], s1[1], s1[2], float(s1[3])],
[s2[0], s2[1], s2[2], float(s2[3])],
[s3[0], s3[1], s3[2], float(s3[3])]
]
# 按成绩降序排序
students.sort(key=lambda x: x[3], reverse=True)
print("排序后:")
for s in students:
print(f"学号:{s[0]}, 姓名:{s[1]}, 成绩:{s[3]}")
测试结果:
