文件操作:
# # 1.文件路径:分为绝对路径和相对路径 # # 2.编码方式:utf-8,jbk..... # # 3.操作方式:只读,只写,追加,读写,写读
1.只读(r,rb);加个b的意思是以bytes类型追加进去(在图片中用)
f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='r', encoding='gbk') content = f.read() print(content) f.close() # f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='rb') content = f.read() print(content) f.close()
2.只写(w,wb),没有文件的话就创建,有就先将源文件内容删除再写
f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='w',encoding='utf-8') content = f.write('内容') print(content) f.close() # f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='w',encoding='utf-8') content = f.write('内容还有内容') f.close() # f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='wb') content = f.write('内容sfs'.encode('utf-8')) f.close()
3.追加(a)
f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='a',encoding='utf-8') content = f.write(',新的东西') f.close() # f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='ab',) content = f.write(',好的天气'.encode('utf-8')) f.close()
4.读写,只能先读再写,否则会出现占位;写读中,同理只能先写再读(w+;还有a+
f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='r+',encoding='utf-8') print(f.read()) print(f.write(',太阳')) f.close() # f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='r+b') print(f.read()) print(f.write(',太阳1'.encode('utf-8'))) f.close()
5.查找时移动光标seek()
f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='r+',encoding='gbk') # content = f.read(2) # 读出来的都是字符 f.seek(2) # 按照字节去定光标的位置 content = f.read() print(content) f.close()
6.告诉你光标的位置tell()
f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='a+',encoding='gbk') f.write('你好') count = f.tell() f.seek(count - 4) # 只读最后两个 print(f.read()) f.close()
7.其他知识点
f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='r+',encoding='gbk') print(f.readable()) # 是否可读readable(),返回值时T或者F print(f.readline()) # 一行一行的读 print(f.readlines()) # 每一行当成列表中的一个元素,添加到列表中 f.truncate() # 截取 f.close()
8.循环读取
f = open('C:/Users/Administrator/Desktop/python例子.txt', mode='r+',encoding='gbk') for i in f: print(i.strip()) # strip去除换行 f.close()
9.with(打开时最后可以不用写close,同时还可以打开多个并进行多种操作,一般都是用的这种打开方式)
with open('C:/Users/Administrator/Desktop/python例子.txt', mode='r+',encoding='gbk') as f, open('C:/Users/Administrator/Desktop/python例子.txt', mode='w+', encoding='gbk') as i: with open('C:/Users/Administrator/Desktop/python例子.txt', mode='r+',encoding='gbk') as f: print(f.read())
10.课后作业:简单的实现用户注册登录
username_registered = input('请输入要注册的用户名') password_registered = input('请输入要注册的密码') with open('C:/Users/Administrator/Desktop/用户信息.txt', mode='w',encoding='utf-8') as f: f.write('{} {}'.format(username_registered,password_registered)) print('恭喜注册成功') i = 0 lis = [] while i < 3: username = input('请输入用户名') password = input('请输入密码') with open('C:/Users/Administrator/Desktop/用户信息.txt', mode='r+',encoding='utf-8') as f1: for k in f1: lis.append(k) if username == lis[0].strip() and password == lis[1].strip(): print('登录成功') break else: print('登录失败,请重新输入') i += 1