Python——文件操作,python文件读取,1. 文件操作基础(
Python——文件操作,python文件读取,1. 文件操作基础(
1. 文件操作基础
(1)文件路径
绝对路径:是从盘符开始的路径,形如C:\windows\system32\cmd.exe
相对路径:是从当前路径开始的路径,假如当前路径为C:\windows,要描述上述路径,只需输入system32\cmd.exe。
实际上,严格的相对路径写法应为.\system32\cmd.exe。其中,.表示当前路径,在通道情况下可以省略,只有在特殊的情况下不能省略。
(2)编码方式
以什么编码方式储存的文件,就以什么方式打开
(3)操作方式
只读、只写、追加、读写【r+,最常用】、写读...
2. 操作方式详解
bytes用于查看非文字类的文件、上传下载储存文件
下面所有例子都由相对路径来描述:
(1)只读:r rb
#正常:r 只读print(‘--------相对路径:只读----------‘)f = open(‘文件‘,mode=‘r‘,encoding=‘utf-8‘) #文件:你好呀content = f.read()print(content) #你好呀f.close()#bytes:rbprint(‘--------相对路径:只读----------‘)f = open(‘文件‘,mode=‘rb‘) content = f.read()print(content) #b‘\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x91\x80‘f.close()
(2)只写:w wb
#正常:w 只写#文件不存在,则创建完了再写print(‘--------相对路径:只写----------‘)f = open(‘file1‘,mode=‘w‘,encoding=‘utf-8‘) #file1不存在f.write("halou") #halouf.close()#文件存在,先删除原文件内容,再写f = open(‘file1‘,mode=‘w‘,encoding=‘utf-8‘) #file存在,内容:halouf.write("辛辰辰") #辛辰辰f.close()#bytes:wb# 文件不存在,则创建完了再写print(‘--------相对路径:只写----------‘)f = open(‘file2‘,mode=‘wb‘)f.write("哈喽halou123".encode(‘utf-8‘)) #需要对写的内容进行编码encode,文件内容:哈喽halou123f.close()#文件存在,先删除原文件内容,再写f = open(‘file2‘,mode=‘w‘,encoding=‘utf-8‘) #file存在,内容:halouf.write("辛辰辰") #辛辰辰f.close()
(3)追加:a ab
#正常:a 追加print(‘--------相对路径:追加----------‘)f = open(‘file1‘,mode=‘a‘,encoding=‘utf-8‘) #file1:辛辰辰f.write(‘追加追加‘) #辛辰辰追加追加f.close()#bytes:abprint(‘--------相对路径:追加----------‘)f = open(‘file2‘,mode=‘ab‘) #file2:辛辰辰f.write(‘zai追加‘.encode(‘utf-8‘)) #辛辰辰zai追加f.close()
(4)读写:r+ r+b
#正常:r+ 读写【最常用】print(‘--------相对路径:读写----------‘)f = open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘) #file:python9print(f.read()) #python9f.write(‘辛辰辰‘) #python9辛辰辰f.close()print(‘--------相对路径:写读----------‘)f = open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘) #file:python9f.write(‘xcc‘) #file:xcchon9print(f.read()) #hon9f.close()#bytes:r+bprint(‘--------相对路径:读写----------‘)f = open(‘file‘,mode=‘r+b‘) #file:python9print(f.read()) #b‘python9‘f.write(‘辛辰辰‘.encode(‘utf-8‘)) #file:python9辛辰辰f.close()print(‘--------相对路径:写读----------‘)f = open(‘file‘,mode=‘r+b‘) #file:python9f.write(‘xcc‘.encode(‘utf-8‘)) #file:xcchon9print(f.read()) #b‘hon9‘f.close()
(5)写读:w+ w+b
#正常:w+ 写读(先清除,再写)print(‘--------相对路径:写读----------‘)f = open(‘file‘,mode=‘w+‘,encoding=‘utf-8‘) #file:python9f.write(‘辛辰辰‘) #辛辰辰print(f.read()) #空f.close()#bytes:w+bprint(‘--------相对路径:写读----------‘)f = open(‘file‘,mode=‘w+b‘) #file:python9f.write(‘辛辰辰‘.encode(‘utf-8‘)) #辛辰辰print(f.read()) #b‘‘f.close()
(6)追加:a+ a+b
#正常:a+ 追加print(‘--------相对路径:追加----------‘)f = open(‘file‘,mode=‘a+‘,encoding=‘utf-8‘) #file:python9f.write(‘辛辰辰‘) #python9辛辰辰print(f.read()) #空f.seek(0) #调整光标位置到开始的地方print(f.read()) #python9辛辰辰f.close()#bytes:a+bprint(‘--------相对路径:追加----------‘)f = open(‘file‘,mode=‘a+b‘) #file:python9f.write(‘辛辰辰‘.encode(‘utf-8‘)) #python9辛辰辰print(f.read()) #b‘‘f.seek(0) #调整光标位置到开始的地方print(f.read()) #b‘python9\xe8\xbe\x9b\xe8\xbe\xb0\xe8\xbe\xb0‘f.close()
3. 功能详解
(1)read
读出来的都是字符,一个字母/汉字
print(‘--------功能详解----------‘)# f = open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘) #file:pythonpythonpythonf = open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘) #file:你好你好你好content = f.read(3) #read读出来的都是字符,一个字母/汉字# print(content) #pytprint(content) #你好你f.close()
(2)seek
按照字节定光标的位置(一个汉字3字节,一个英文字母1字节)
print(‘--------功能详解----------‘)# f = open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘) #file:PYthonpyTHonpythONf = open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘) #file:你好呀辛辰f.seek(3) #光标:按照字节定位置content = f.read()# print(content) #honpyTHonpythON:光标位置再三个字节之后(utf-8中:一个字母一个字节)print(content) #好呀辛辰:光标位置再三个字节之后(utf-8中:一个汉字三个字节)f.close()
(3)tell
监测光标的位置
print(‘--------功能详解----------‘)f = open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘) #file:PYthonpyTHonpythON# f = open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘) #file:你好呀辛辰f.seek(3)f.tell() #监测光标的位置print(f.tell()) #3# print(f.read()) #从第三个字节后面开始读取:好呀辛辰print(f.read()) #从第三个字节后面开始读取:honpyTHonpythONf.close()
应用举例:
f = open(‘file‘,mode=‘a+‘,encoding=‘utf-8‘) #file:python9f.write(‘辛辰辰‘) #python9辛辰辰count = f.tell()f.seek(f.tell()-3)print(f.read()) #辰(后三个字节表示一个汉字)f.close()#读取文件file中的从第3个字节开始的连续5个字节的内容f = open(‘file‘,mode=‘a+‘,encoding=‘utf-8‘) #file:python9辛辰辰f.seek(2)print(f.read(5)) #thon9(python9辛辰辰)f.close()
(4)with open
使用with open打开文件,可以不用写 f.close()
#打开一个文件with open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘) as f: print(f.read()) #这是file1文件#打开多个文件with open(‘file1‘,mode=‘r+‘,encoding=‘utf-8‘) as f1,open(‘file2‘,mode=‘w+‘,encoding=‘utf-8‘) as f2: print(f1.read()) #这是file1文件 f2.write(‘哈喽‘) #file2:哈喽 print(f2.read()) #空:w+是先清除再写
(5)others
f = open(‘file‘,mode=‘r+‘,encoding=‘utf-8‘)f.readable() #是否可读print(f.readable()) #Trueline = f.readline() #读一行print(line) #这是file文件1lines = f.readlines() #把每一行当成列表中的一个元素,添加到list中print(lines) #[‘这是file文件2\n‘, ‘这是file文件3\n‘, ‘这是file文件4\n‘, ‘这是file文件5‘]# f.truncate(5) #截取前五位f.close()
Python——文件操作
相关内容
- python简单实用gunicorn部署,部署python,linux 安装 p
- Python+matplotlib+scipy站点数据绘制气象分布图,matplotlib
- 我把全国旅游数据用Python爬下来后发现,这个地方才是
- python pdfplumber用于pdf表格提取,python读取pdf表格, 1 imp
- Python绘制五星红旗,什么的五星红旗,# -*- codi
- 如何让你的Python代码更加pythonic ?,怎样让代码高亮显
- python安装第三方模块,python第三方库安装,模块分类1、
- Python安装numpy模块及pycharm配置,,查看Python版本
- Python学习-range的用法,,range() 函数
- 用python读取带密码的excel文件中的数据,python读取exce
评论关闭