一:今日作业:
1、编写文件copy工具
old_file = input("请输入源文件的绝对路径:").strip()
new_file = input("请输入新文件的绝对路径:").strip()
with open(old_file, "rt", encoding="utf-8") as f1:
with open(new_file, "wt", encoding="utf-8") as f2:
info = f1.read()
f2.write(info)
print("copy成功")
print("新文件路径为:", new_file)
2、编写登录程序,账号密码来自于文件
inp_user = input("请输入账号")
inp_psw = input("请输入密码")
with open(r"D:工具pythonpython代码练习day11aaa.txt",
mode="r",encoding="utf-8") as f:
for line in f:
user, password = line.strip().split(":")
if inp_psw == password and inp_user == user:
print("登陆成功")
break
else:
print("登录失败,账号或密码错误")
3、编写注册程序,账号密码来存入文件
inp_user = input("请输入想注册的账号")
inp_psw = input("请输入密码")
with open(r"D:工具pythonpython代码练习day11aaa.txt",
mode="r", encoding="utf-8") as f:
for line in f:
user = line.strip().split(":")[0]
if inp_user == user:
print("账号名已存在,不能注册")
break
else:
with open(r"D:工具pythonpython代码练习day11aaa.txt",
mode="a", encoding="utf-8") as f:
f.write("{}:{}
".format(inp_user, inp_psw))
print("注册成功")