# coding:UTF-8 # @author:lsj # @version:1.0 #1、通用文件copy工具实现 # src_file = input("请输入源文件路径:").strip() # des_file = input("请输入目标文件路径:").strip() # with open(r"{}".format(src_file),mode="rb") as f1,\ # open(r"{}".format(des_file),mode='wb') as f2: # for line in f1: # f2.write(line) #2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容 # 前提:创建一个a.txt文件,里面的内容是"abc你们好" # 测试r+ # with open("a.txt",mode="r+",encoding="utf-8") as f: # print(f.tell()) # 0 指针在文件开头 # print(f.read()) # abc你们好 文件全部内容 # # res = f.seek(0,0) # 0 指针在文件开头 # res = f.seek(0,1) # 0 指针在文件开头 # print(res) # print(f.tell()) # 9 # 测试w+ # with open("a.txt",mode="w+",encoding="utf-8") as f: # print(f.read()) # io.UnsupportedOperation: not readable不支持读 # f.write("abc你好") # f.seek(2,0) # print(f.tell()) # 2 # print(f.seek(2,0)) # 2 # f.seek(2,1) #报错 io.UnsupportedOperation: can't do nonzero cur-relative seeks # print(f.tell()) # f.seek(2,2) # 报错:io.UnsupportedOperation: can't do nonzero end-relative seeks # 测试a+ # with open("a.txt",mode="a+",encoding="utf-8") as f: # f.read() # print(f.read()) # # f.write("abc你好") # f.seek(2,0) # # 2 # print(f.tell()) # f.seek(2,1) #报错 io.UnsupportedOperation: can't do nonzero cur-relative seeks # print(f.tell()) # f.seek(2,2) # 报错 # # 3、tail -f access.log程序实现 # 永远获取日志的最后一行。 # with open('access.log',mode='rb') as f: # f.seek(0,2) # while True: # line=f.readlines() # if len(line) == 0: # pass # else: # print(line.decode('utf-8'),end='') #4、周末作业参考在老师讲解完毕后(下午17:30开始讲解),练习熟练,明早日考就靠他俩 # 4.1:编写用户登录接口 # username = input("请输入您的账号:").strip() # password = input("请输入密码:").strip() # with open(r'user.txt',mode='rt',encoding="utf-8") as f: # for line in f: # user,pwd = line.strip().split(":") # if user == username and pwd == password: # print("登录成功!!") # break # else: # print("登录失败!!!") # 4.2:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件) # username = input("请输入您的注册账号:").strip() # password = input("请输入您的注册密码:").strip() # with open("login.txt",mode="wt",encoding="utf-8") as w: # w.write("{}:{}\n".format({username},{password})) dic = { "0":"退出", "1":"登录", "2":"注册" } while True: msg = """ 0 退出 1 登录 2 注册 """ print(msg) cmd = input('请输入命令编号>>: ').strip() if not cmd.isdigit(): print('必须输入命令编号的数字,傻叉') continue # 判断当前用户如果不在dic字典中,执行 if cmd not in dic: print('输入的命令不存在') continue if cmd == "0": print("退出") break if cmd == "1": username = input("请输入您的账号:").strip() password = input("请输入密码:").strip() with open('login.txt',mode='rt',encoding="utf-8") as f: for line in f: user,pwd = line.strip().split(":") if user == username and pwd == password: print("登录成功!!") break else: print("登录失败!!!") if cmd == "2": username = input("请输入您的注册账号:").strip() password = input("请输入您的注册密码:").strip() with open("login.txt",mode="at",encoding="utf-8") as w: w.write("{}:{}\n".format(username,password)) print("注册成功可以登录!!!") print(dic.get(cmd))
Python学习0316作业
猜你喜欢
转载自www.cnblogs.com/liunaixu/p/12508011.html
今日推荐
周排行