Python学习————文件处理

1、编写文件copy工具

src_file = input("输入文件路径:").strip()
new_file = input("输入新文件路径:").strip()
with open(f'{src_file}', mode="rt", encoding="utf-8") as f1, \
        open(f'{new_file}', mode="wt", encoding="utf-8") as f2:
    res = f1.read()
    f2.write(res)

2、编写登录程序,账号密码来自于文件

name = input("请输入账号:").strip()
password = input("请输入密码:").strip()
with open("username.txt", mode="rt", encoding="utf-8") as f:
    for i in f:
        username, userpassword = i.strip().split(":")
        if username == name and userpassword == password:
            print("密码正确,登录成功")
            break
    else:
        print("账号或密码错误,登陆失败")

3、编写注册程序,账号密码来存入文件

print('用 户 注 册')
for i in range(3):
    name = input("输入账号\n ").strip()
    password = input("输入密码\n ").strip()
    if name == password:
        print('账号与密码相同,注册失败')
    else:
        print('注册成功')
        with open("username.txt", mode="at", encoding="utf-8") as f:
            f.write(f'{name}:{password}\n')
            break
posted @ 2020-03-13 18:49  Dimple_Y  阅读(166)  评论(0编辑  收藏  举报