1、编写文件copy工具
#!/usr/bin/env python #-*- coding:utf-8 -*- import os def main(): while 1: source_path = input('请输入要文件拷贝的源路径:').strip() if os.path.exists(source_path): obj_path = input('请输入文件拷贝的目的路径:').strip() f = open(source_path,encoding='utf-8',mode='r') f2 = open(obj_path, encoding='utf-8', mode='w') str = f.read() f2.write(str) f.close() f2.close() break else: print('输入的源地址文件不存在,请重新输入!') if __name__ == '__main__': main()
2、编写登录程序,账号密码来自于文件
user_info = {}
count = 0
with open("/home/day10/user.db",mode="r",encoding="utf-8") as f:
for line in f:
user_name, passwd = line.strip().split(":")
info[user_name] = passwd
while count<3:
name = input("请输入你的用户名:").strip()
pwd = input("请输入你的密码:").strip()
if name in user_info.keys() and pwd == user_info.get(name):
print("用户{}登录成功!".foramt(name))
break
else:
count +=1
print("用户名或者密码错误")
else:
print("用户名或密码输错三次,退出")
3、编写注册程序,账号密码来存入文件
user_info = {} # 输入注册账号密码 name = input("输入账号名:").strip() pwd = input("输入账号密码:").strip() # 读取文件中已存在的账号密码信息 with open("/home/day10/user.db",mode="r",encoding="utf-8") as f: for line in f: user_name, passwd = line.strip().split(":") user_info[user_name] = passwd # 判断用户名是否存在,存在就添加到文件中 if name in info: print("用户名已被使用") else: _name = "%s:%s "%(name,pwd) with open("/home/day10/user.db", "a", encoding="utf-8") as f: f.write(_name) print("注册成功。")