本文共 3367 字,大约阅读时间需要 11 分钟。
文件操作
文件上传 open()系统函数
open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True) --->返回值:stream(管道)
读图片音乐啥的用rb,以二进制形式读取
缓存:
类比内存,内存起的就是缓存作用
CPU从内存读文件,硬盘暂存在内存里
缓存同理
自己定义的函数里面加三个引号,默认会产生值
mode形式 --->指明图解中管道的类型 如:是读还是写
'r' open for reading (default) 'w' open for writing, truncating the file first 'x' create a new file and open it for writing 'a' open for writing, appending to the end of the file if it exists 'b' binary mode 't' text mode (default) '+' open a disk file for updating (reading and writing) 'U' universal newline mode (deprecated)open()def sum(n): ''' :param n: :return: '''
tip:ctrl+鼠标左键单击打开内置函数帮助,在打开界面停留会跳出说明表
tip: mac的话 路径建议不要使用\,使用/
读操作
stream.read() 读取所有内容
stream=open(r'C:\Users\用户\Desktop\aa.txt') #r保留原格式'''#写绝对路径,桌面要创建一个aa.txt文件#若改为stream=open(r'C:\Users\gszhu\Desktop\aa1.txt')#报错 FileNotFoundError: [Errno 2] No such file or directory:'C:\\Users\\gszhu\\Desktop\\aa1.txt''''container= stream.read() #读取流(管道)里面的东西print(container)'''hello worldhello tuotuohello ye#txt文本里写的就是这些内容'''
tip:同级目录可以直接写文件名,不是同级目录写完整的绝对路径
注意:如果传递的path/filename有误,报错:FileNotFoundError
stream=open(r'C:\Users\用户\Desktop\a1.docx')container= stream.read() #读流里面的东西print(container)'''UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 50: illegal multibyte sequence'''
stream.readable() 判断是否可以读取,可以返回True
stream.readline() 每次读取一行内容
stream=open(r'C:\Users\gszhu\Desktop\aa.txt')#写绝对路径container= stream.read() #读流里面的东西print(container)result=stream.readline()print(result)'''hello worldhello tuotuohello ye'''
txt内容被read读完了,所以readline读不到内容
修改:
stream=open(r'C:\Users\gszhu\Desktop\aa.txt')result=stream.readline()print(result)'''hello world'''
如何用readline把所有内容都读出来
stream=open(r'C:\Users\gszhu\Desktop\aa.txt')while True: result=stream.readline() #读行,默认换行了 print(result) if not result: #取的内容为空 break'''hello worldhello tuotuohello ye'''
stream.readlines() 读取所有行,保存到列表中
stream=open(r'C:\Users\gszhu\Desktop\aa.txt')result=stream.readlines()print(result)for i in result: #遍历列表逐行取出 print(i)'''['hello world\n', 'hello tuotuo\n', 'hello ye'] #\n为换行hello worldhello tuotuohello ye'''
readline和readlines只能读字符串,而read可以读所有内容
读图片 不能使用默认的读取方式(mode默认为'rt')
stream=open(r'C:\Users\gszhu\Desktop\csdn.jpg')#不报错container=stream.read() #报错 默认读的文档print(container)'''UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence'''
解决方法
stream=open(r'C:\Users\gszhu\Desktop\csdn.jpg','rb') #用rb读container=stream.read() print(container)'''b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\... 二进制'''
写操作 mode为'w'
stream=open(r'C:\Users\gszhu\Desktop\aa.txt','w')s=''' 黑色的不是夜晚是漫长的孤单看脚下一片黑暗望头顶星光璀璨'''#三引号保留格式container=stream.write(s)print(container)stream.close()#做完操作后close一下,释放流资源(把管道断了回收)'''34'''
注:第一行是空的
stream=open(r'C:\Users\gszhu\Desktop\aa.txt','w')s=''' 黑色的不是夜晚是漫长的孤单'''#三引号保留格式container=stream.write(s)print(container)stream.write('看脚下一片黑暗,望头顶星光璀璨')stream.write('yi')stream.close()#做完操作后close一下,释放流资源#stream.write('yi') ValueError: I/O operation on closed file.
注:每次进行写操作,都会把原来的内容先清空再写入当前的内容
之前保存的内容为上一段代码写入的内容
写的时候接在后面写,此处yi接在后面写了,怎么换行?
writelines(Interable) 没有换行效果
stream.writelines(['刺猬\n','小虎\n','熊猫\n']) 可以迭代
mode='a'模式
不清空,直接追加
开始:
代码:
stream=open(r'C:\Users\gszhu\Desktop\aa.txt','a')stream.write('yi')stream.writelines(['刺猬\n','小虎\n','熊猫\n'])stream.close()
结果:
转载地址:http://azegn.baihongyu.com/