1 文件的基本操做python
#1. 打开文件的模式有(默认为文本模式): r ,只读模式【默认模式,文件必须存在,不存在则抛出异常】 w,只写模式【不可读;不存在则建立;存在则清空内容】 a, 只追加写模式【不可读;不存在则建立;存在则只追加内容】 #2. 对于非文本文件,咱们只能使用b模式,"b"表示以字节的方式操做(而全部文件也都是以字节的形式存储的,使用这种模式无需考虑文本文件的字符编码、图片文件的jgp格式、视频文件的avi格式) rb wb ab 注:以b方式打开时,读取到的内容是字节类型,写入时也须要提供字节类型,不能指定编码 #3,‘+’模式(就是增长了一个功能) r+, 读写【可读,可写】 w+,写读【可写,可读】 a+, 写读【可写,可读】 #4,以bytes类型操做的读写,写读,写读模式 r+b, 读写【可读,可写】 w+b,写读【可写,可读】 a+b, 写读【可写,可读】
2 文件的方法vim
read(3): 1. 文件打开方式为文本模式时,表明读取3个字符 2. 文件打开方式为b模式时,表明读取3个字节 其他的文件内光标移动都是以字节为单位的如:seek,tell,truncate 注意: 1. seek有三种移动方式0,1,2,其中1和2必须在b模式下进行,但不管哪一种模式,都是以bytes为单位移动的(如是移动中文的话,在utf-8 中移动,若不是3的倍数,就会报错) 2. truncate是截断文件,因此文件的打开方式必须可写,可是不能用w或w+等方式打开,由于那样直接清空文件了,因此truncate要在r+或a或a+等模式下测试效果。(不知道可不能够调光标)
r 读模式:app
若在E盘下的能够把文件的相对路径写入以下:(不存在此文件时,会报错)编辑器
f = open('e:/new.txt',mode = 'r', encoding='utf-8')#编码用什么方式编的, 就要用什么方式打开,在pyCharm 中默认是用utf-8 编写的 conenct = f.read() print(conenct) f.close()
f = open('newt',mode = 'r', encoding='utf-8')#编码用什么方式编的, 就要用什么方式打开,在pyCharm 中默认是用utf-8 编写的 conenct = f.read() print(conenct) f.close()
这是打开python 文件下目录下文件, 就不用写绝对路径,只须要定相对路径便可测试
注意 :在文件操做时必定要记得关闭文件,也就是close编码
rb bytes模式下的读(非文本类型的时候用)spa
f = open('file1',mode='rb') conenct = f.read() print(conenct)#b'\xe6\x88\x91\xe6\x83\xb3\xe7\x9d\xa1\xe8\xa7\x89' f.close()
由于是用 bytes 类型打开,因此就不用写解码方式,打印的数据是 utf-8 ,由于我写了四个中文,12个字节code
w 写模式orm
不存在就建立,存在就全删在写视频
f = open('file1',mode='w',encoding='utf-8') conenct = f.write('我想睡觉') print(conenct)#返回值为其的字符个数,只能写,覆盖,不能读 f.close()
wb bytes模式下的写
f = open('file1',mode='wb') conenct = f.write('傻不傻'.encode('utf-8')) print(conenct)#返回的其字节个数 9 f.close()
a 追加
其实和w 差很少, 只是不会删除,在以后写上而以(ab 都同样,和wb 差很少)
r+ 读写模式(在读的基础上加了写的功能,可是只有两个功能, 再读的话就不行了)这个功能是用的最多的
w+ 与a+
f = open('file1',mode='w+',encoding='utf-8') f.write('afdf') f.seek(0)#要移动光标才能读出来东西 print(f.read(2)) f.close()
3 对文件的修改
将硬盘存放的该文件的内容所有加载到内存,在内存中是能够修改的,修改完毕后,再由内存覆盖到硬盘(word,vim,nodpad++等编辑器)
import os # 调用系统模块 with open('a.txt') as read_f,open('.a.txt.swap','w') as write_f: data=read_f.read() #所有读入内存,若是文件很大,会很卡 data=data.replace('alex','SB') #在内存中完成修改 write_f.write(data) #一次性写入新文件 os.remove('a.txt') #删除原文件 os.rename('.a.txt.swap','a.txt') #将新建的文件重命名为原文件
方法一
方式二:将硬盘存放的该文件的内容一行一行地读入内存,修改完毕就写入新文件,最后用新文件覆盖源文件
import os with open('a.txt') as read_f,open('.a.txt.swap','w') as write_f: for line in read_f: line=line.replace('alex','SB') write_f.write(line) os.remove('a.txt') os.rename('.a.txt.swap','a.txt') 方法二
ps:
之后打开方式用 with 能够不用 close
with open('log',mode='r+',encoding='utf-8') as f,open('log',mode='w+',encoding='utf-8') as f1:
也能够打开多个文件
4 注册小做业
一个简单的注册登入做业,若是没有帐号就建立一个新的,有就用原来的帐号登入,
flag = True while flag: i = 0 while i <3 and flag : whether_account = input('是否拥有帐号,若是有请输入 y,没有输入 n(输入 q 退出):') if whether_account.strip() == 'y': break elif whether_account.strip() =='n': username = input('请输入想要注册的用户名:') password = input('请输入密码:') with open(username, mode='w+', encoding='utf-8') as f1: f1.write('{}\n{}'.format(username,password)) break elif whether_account.strip() =='q': flag =False elif i !=2: print('请输入正确字符,你只有{}机会'.format(2-i)) i +=1 lis = [] i = 0 try: while i < 3 and flag: user_name = input('请输入你的帐号:') password = input('请输入你的密码:') with open(user_name, mode='r+', encoding='utf-8') as f2: for lin in f2: lis.append(lin.strip()) if user_name == lis[0] and password == lis[1]: print('欢迎登入'.center(51,'~')) flag = False elif i != 2: print(' 你只有 %d 次机会' %(2-i)) i += 1 except(FileNotFoundError) as e: c