python-文件操作
'''文件操作
打开文件:open
读写文件
读:把文件的内容读到变量-read里面
写:把变量值写入到文件里面
关闭文件'''
#读取文件
'''
打开文件
变量名=open(要打开的文件路径,"r")
变量名一定要存在:代表操作文件的对象后面要将文件内容读入到变量里面
open:后的第一个参数(文件路径)一定要存在,如果文件不存在会报错
”r“的意思就是:用只读的方式打开文件'''
'''读取文件内容
txt=file.read()
txt:也是一个变量名,用于存储读取到的文件内容
file:也是一个变量名,是open返回的问价内容
.read()是读取文件的方式'''
'''关闭文件
file.close()
文件打开后一定要进行关闭'''
示例
1 2 3 4 5 6 | #示例 file = open (r "D:\File\pythonProject\weidong.txt" , "r" ,encoding = "gbk" ) txt = file .read() print (txt) file .close() '''注意:如果在文件中有中文存在,那么需要在open里面加一个encoding="utf8"将文件编码定位utf8才可以正常读取,否则会报错,如果全是英文那么不用修改使用原有的gbk就可以''' |
#写入第一个文件
'''操作步骤
打开文件
与读取文件不同的点在写入文件要将后面跟的”r“改为”w“,
注意:前面的r意思是为例方式出现转义的情况
使用W写入文件,文件名不存在的话会创建一个新文件,文件名存在的话会覆盖原有文件
写入文件
file.write(需要写入的内容)
关闭文件
file.close()'''
1 2 3 4 5 6 7 8 9 10 11 | file = open (r "D:\File\pythonProject\weidong.txt" , "w" ) file .write( "zheshi yigebijiaomafandeneirong" ) file .close() '''注意:通上一致打开文件时如果有中文就得指定字符集encoding=”utf8“''' '''open的第二个参数分类 r:只读 w:只写(会覆盖原有内容) a:追加(不会覆盖原有内容) rb:二进制只读 wb:二进制只写 ab二进制追加''' |
#修改文件
'''操作步骤:
将原有文件使用r的方式读取
在使用字符串修改方法修改文件内容,将修改后的内容存储到变量里面
关闭文件(在这个地方必须关闭文件)
使用w的方式打开文件
将变量里面的值写入到文件里面'''
#示例
1 2 3 4 5 6 7 | file = open (r "D:\File\pythonProject\a.txt" , "r" ,encoding = "gbk" ) tet = file .read() a = tet.replace( "我爱你" , "love" ) file .close() file = open (r "D:\File\pythonProject\a.txt" , "w" ) file .write(a) file .close() |
#复制文件
'''把文件1打开读取到变量里面
关闭文件1
打开文件2,使用w的方式
写入文件
关闭文件'''
1 2 3 4 5 6 | file = open (r "D:\File\pythonProject\a.txt" , "r" ) tewt = file .read() file .close() file = open (r "D:\File\pythonProject\b.txt" , "w" ) file .write(tewt) file .close() |
#合并文件
'''实现原理
把文件1使用r的方式读取到变量中,关闭文件1
把文件2使用r的方式读取到变量中,关闭文件2
使用w都得方式打开文件3,将文件1+文件2写入到文件3中
关闭文件'''
1 2 3 4 5 6 7 8 9 | file = open (r "D:\File\pythonProject\a.txt" , "r" ) trt = file .read() file .close() file = open (r "D:\File\pythonProject\b.txt" , "r" ) tqt = file .read() file .close() file = open (r "D:\File\pythonProject\c.txt" , "w" ) file .write(trt + tqt) file .close() |
readline按行读取文件
'''由于read一次性会把文件中所有内容读取到内存里面,如果文件特别大会消耗内存
所以可以使用readline()按行读取文件
使用readline()后每调用一次,内部文件指针就会下降一行,,这样的结果是下次再次调用readline会自动读取下一行
readline读取到文件最后,返回""
如果要通过readline读取文件内容
可以写一些死循环
在循环内部调用readline,读取文件
如果readline返回""
则终止这个循环'''
1 2 3 4 5 6 7 | file = open (r "D:\File\pythonProject\c.txt" , "r" ) while True : txt = file .readline() if txt = = "": break print (txt,end = "") file .close() |
#读取文件的偶数行
'''思路:写一个死循环,使用循环计数
如果循环计数的变量和2取余数为0
那么往变量里面写如当前循环的文件行的内容'''
1 2 3 4 5 6 7 8 9 10 | file = open (r "D:\File\pythonProject\c.txt" , "r" ) index = 1 #注意循环计数起始值都为0,为防止漏泄需要与readline读取到的的行数保持一致 while True : tet = file .readline() if tet = = "": break if index % 2 = = 0 : print (tet) index + = 1 file .close() |
#readlines()
'''一下子读取文件所有行,返回一个列表,列表中的一个成员就是文件中的一行,文件有多少行,列表就有多少成员'''
1 2 3 4 5 6 | file = open (r "D:\File\pythonProject\c.txt" , "r" ) twt = file .readlines() #返回的是一个列表 print (twt) #查看列表中的成员 for n in twt: #遍历列表,查看文件有多少行 print (n) file .close() |
with open语法
'''with open
时读写文件的一种简化写法
不需要明确关闭文件'''
1 2 3 | with open (r "D:\File\pythonProject\c.txt" , "r" ) as file : txt = file .read() print (txt) |
json文件操作
#json文件
'''一般定义数据使用
在互联网传递数据的时候很常见的一种文件格式
所有数据用一对大括号括起来
大括号内部是键值对
键和值要用冒号分隔
字符串用双引号
数字不需要引号
列表用中括号
对象用大括号'''
#读写json文件
'''一;导入json模块
二:打开json文件
三:调用json模块中的load方法,读取文件
四关闭打开文件'''
1 2 3 4 5 6 | import json with open (r "D:\File\pythonProject\d" , "r" ,encoding = "utf8" ) as file : txt = json.load( file ) for n in txt: print (n,txt[n]) '''注意字符集,json文件中有中文一定要指定字符集utf8''' |
#写如json文件
'''导入json模块
使用只写的方式打开json文件
调用json模块中的dump方法把字典写入文件
ensure_ascii = False代表中文不转义
关闭文件'''
1 2 3 4 5 6 7 | use = { "name" : "anlun" , "age" : 60 , "addre" : [ 1 , 2 , 3 ] } with open (r "D:\File\pythonProject\d" , "w" ,encoding = "utf8" ) as file : json.dump(use, file ,ensure_ascii = False ) |
#修改json文件中的值
'''修改字典值的方法'''
1 2 3 4 5 | with open (r "D:\File\pythonProject\f" , "r" ,encoding = "utf8" ) as file : twt = json.load( file ) twt[ "age" ] = 22 with open (r "D:\File\pythonProject\f" , "w" ,encoding = "utf8" ) as file : json.dump(twt, file ,ensure_ascii = False ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现