文件操作
一.文件操作的流程
1. 建立文件
2. 建立文件对象,就是打开文件open函数,即打开文件句柄
3.模式操作,如读r,写w
4.调用方法,如read()
5.关闭文件(.close把缓冲区的数据写入到磁盘,即使不手动做,python也会自动执行,前提是程序执行完,但是建议每次都手动加上)
注意:能调用方法的一定是对象,文件也是对象

In [6]: l1=[1,2,3] #对象
In [7]: l1.append('4') #调用的方法
In [8]: l1
Out[8]: [1, 2, 3, '4']

  

二.文件操作的模式
注:读模式只能读,写模式只能写

[root@zabbix day8]# cat file.txt
hello reid

1.读r:

In [15]: f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
In [16]: data=f.read(5) ##5显示多少个字符,在Py3中文和英文一样,默认是取所有
In [17]: print(data)
hello

  

2.写w:先把之前的清空,再写

[root@zabbix day8]# cat file-operate.py
#!/usr/local/python3/bin/python3
f=open('/root/py/fullstack_s2/week2/day8/file.txt','w')      ##对象创建时,就已经把文件内容清空,文件不存在就会创建文件
data=f.write('hello world')
print(data)
f.close()
[root@zabbix day8]# python3 file-operate.py
11
[root@zabbix day8]# cat file.txt
hello world[root@zabbix day8]###连续写不换行
[root@zabbix day8]# cat file-operate.py
#!/usr/local/python3/bin/python3
f=open('/root/py/fullstack_s2/week2/day8/file1.txt','w')
f.write('hello world')
f.write('tom')
f.close()
[root@zabbix day8]# python3 file-operate.py
[root@zabbix day8]# cat file1.txt
hello worldtom[root@zabbix day8]# 

分析:文件在内存里,如划分成一行行的,每行里还划分成一个个格,如存一个hello,一个个单位的存储,存完后,如果后面还有存储它就会接着存,不会有任何东西,如果需要空格可以在里面加空格,然后空格就占用一个位置,可以加换行符,如果不加就会走到行的最大量才换.实际上python有一个指针,如在写文件时,都在开始处,如写hell,指针一开始在h,写到l时就停在l,再写其他就从l开始写

f.write('hello world \n')
f.write('tom')

3.追加a:在光标所在处开始追加

[root@zabbix day8]# cat file-operate.py
#!/usr/local/python3/bin/python3
f=open('/root/py/fullstack_s2/week2/day8/file1.txt','a') ##a追加
f.write('\n hello world \n')
f.write('tom')
f.close()[root@zabbix day8]# cat file1.txt
hello worldtom[root@zabbix day8]# [root@zabbix day8]# python3 file-operate.py
[root@zabbix day8]# cat file1.txt
hello worldtom
hello world

4. r+ 读写,光标默认在起始位置,常用的模式***

[root@zabbix day8]# cat file1.txt
hi reid
hello world
In [30]: f=open('/root/py/fullstack_s2/week2/day8/file1.txt','r+')
In [31]: print(f.readline())
hi reid
In [32]: f.write('tom')
Out[32]: 3
[root@zabbix day8]# cat file1.txt
hi reid
hello world
tom[root@zabbix day8]#

  

5. w+ 写读

[root@zabbix day8]# cat file1.txt
hi reid
hello world
In [34]: f=open('/root/py/fullstack_s2/week2/day8/file1.txt','w+') ##打开后,就直接格式化之前的内容
In [35]: print(f.readline()) ##直接读时为空
In [36]: f.write('tom') ##写入新内容
Out[36]: 3
In [37]: print(f.tell()) ##写入新内容后光标所在3,再读也是空
3
In [38]: print(f.readline())
In [39]: f.close()
[root@zabbix day8]# cat file1.txt
tom[root@zabbix day8]#

把光标重新移动:

[root@zabbix day8]# cat file1.txt
tom
In [41]: f=open('/root/py/fullstack_s2/week2/day8/file1.txt','w+')
In [42]: f.write('reid \n')
Out[42]: 6
In [43]: print(f.readline())
In [44]: f.seek(0)
Out[44]: 0
In [45]: print(f.readline())
reid
In [46]: f.close()

  

6. a+ 追加可读,光标默认是在最后

[root@zabbix day8]# cat file1.txt
reid
In [47]: f=open('/root/py/fullstack_s2/week2/day8/file1.txt','a+')
In [48]: print(f.tell())
6
In [49]: print(f.readline())

  

三.文件操作具体方法

1.readline按行读

[root@zabbix day8]# cat file-operate.py
#!/usr/local/python3/bin/python3
f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
print(f.readline())
print(f.readline())
f.close()[root@zabbix day8]# python3 file-operate.py
I heard the echo, from the valleys and the heartOpen to the lonely soul of sickle harvesting

  

2.read按字符读

[root@zabbix day8]# cat file-operate.py
#!/usr/local/python3/bin/python3
f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
print(f.read(5))
print(f.read(5))
f.close()
[root@zabbix day8]# python3 file-operate.py
I hea 读到5个字符时,还有一个换行符
rd th

  

3.readlines输出列表

In [1]: f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
In [2]: print(f.readlines())
['I heard the echo, from the valleys and the heart\n', 'Open to the lonely soul of sickle harvesting\n', 'Repeat outrightly, but also repeat the well-being of\n', 'Eventually swaying in the desert oasis\n']

  

4.使用for循环使用readlines直接读列表里的内容

In [3]: f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
In [4]: for i in f.readlines():
...: print(i)
...:
I heard the echo, from the valleys and the heart       ##打印时本身就有一个换行,再加print默认有个换行Open to the lonely soul of sickle harvestingRepeat outrightly, but also repeat the well-being ofEventually swaying in the desert oasis

strip()把末尾的换行自动去掉

In [7]: f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
In [8]: for i in f.readlines():
...: print(i.strip())
...:
I heard the echo, from the valleys and the heart
Open to the lonely soul of sickle harvesting
Repeat outrightly, but also repeat the well-being of
Eventually swaying in the desert oasis

  

5.实现处理文件中某一行,增加一个字符串

#!/usr/local/python3/bin/python3
num=0
f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
for i in f.readlines():num +=1             ##每读一行就加1if num == 2:        ##读到第2行时就加上需要加的字符串print(i.strip(),'======reid') else:print(i.strip())
f.close()
[root@zabbix day8]# python3 file-operate.py
I heard the echo, from the valleys and the heart
Open to the lonely soul of sickle harvesting ======reid
Repeat outrightly, but also repeat the well-being of
Eventually swaying in the desert oasis

优化一:

[root@zabbix day8]# cat file-operate.py
#!/usr/local/python3/bin/python3
num=0
f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
for i in f.readlines():num +=1if num == 2:i='+++++'.join((i.strip(),'reid'))   ##直接使用i=,节省了一个Print,字符串拼接时使用joinprint(i.strip())
f.close()
[root@zabbix day8]# python3 file-operate.py
I heard the echo, from the valleys and the heart
Open to the lonely soul of sickle harvesting+++++reid
Repeat outrightly, but also repeat the well-being of
Eventually swaying in the desert oasis

优化二:

[root@zabbix day8]# cat file-operate.py
#!/usr/local/python3/bin/python3
f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
data=f.readlines()
#只把文件读了一下,把内容取出来,马上就关闭,剩下就自己处理,使用文件的时间短,优化一是整个操作都在文件打开过程中,看实际,如果频繁操作效率不高
f.close
num=0
for i in data:num +=1if num == 2:i='+++++'.join((i.strip(),'reid'))print(i.strip())
f.close()
[root@zabbix day8]# python3 file-operate.py
I heard the echo, from the valleys and the heart
Open to the lonely soul of sickle harvesting+++++reid
Repeat outrightly, but also repeat the well-being of
Eventually swaying in the desert oasis##不使用定义变量:num,使用enumerate来接收两个变量
[root@zabbix day8]# cat file-operate.py
#!/usr/local/python3/bin/python3
f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
for i,v in enumerate(f.readlines()):if i == 2:v='   ******'.join((v.strip(),'reid'))print(v.strip())
f.close()
[root@zabbix day8]# python3 file-operate.py
I heard the echo, from the valleys and the heart
Open to the lonely soul of sickle harvesting
Repeat outrightly, but also repeat the well-being of   ******reid
Eventually swaying in the desert oasis

优化三:使用列表直接修改

In [9]: f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
In [10]: data=f.readlines()
In [11]: data[1]='Open to the lonely soul of sickle harvesting ppppppppppp'
In [12]: print(data)
['I heard the echo, from the valleys and the heart\n', 'Open to the lonely soul of sickle harvesting ppppppppppp', 'Repeat outrightly, but also repeat the well-being of\n', 'Eventually swaying in the desert oasis\n']

优化四: 对readlines的优化方式:**** 推荐用法
注:readlines是把数据都读到内存里,如果文件大的话一下子就会崩溃

In [1]: f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
In [2]: for i in f: #文件本身在磁盘上,for i in f时没有对磁盘作任何操作...:     print(i.strip()) ##内部,python会对for做了特殊处理,把f做成迭代器,它会用时取一个,不用就不取,对于文件本用一行取一行,数据在磁盘,使用时在内存,使用算法计算实现...:
I heard the echo, from the valleys and the heart
Open to the lonely soul of sickle harvesting
Repeat outrightly, but also repeat the well-being of
Eventually swaying in the desert oasi

对某行处理:

[root@zabbix day8]# cat file-operate.py
#!/usr/local/python3/bin/python3
f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
num=0
for i in f:num +=1if num == 2:i='+++++'.join((i.strip(),'reid'))print(i.strip())
f.close()
[root@zabbix day8]# python3 file-operate.py
I heard the echo, from the valleys and the heart
Open to the lonely soul of sickle harvesting+++++reid
Repeat outrightly, but also repeat the well-being of
Eventually swaying in the desert oasis  

summary: readlines与迭代器对比
readlines: 有一个文件,内容一行行在磁盘里存储着,使用readlines是完全把整个文件copy到内存里,对它进行操作,如果文件的太大就会影响性能
迭代器的实现:做一个迭代器在内存里,当使用第一行内容时,就取第一行的内容,取到放在内容后,可以对它进行应用,如print下,print完后,内存就没有相当于del,是解释器做的,再到下一行,整个处理的过程中只有一行的内容

 

6.tell: 用于检测当前光标的位置

In [1]: f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
In [2]: print(f.tell()) #开始是0
0
In [3]: print(f.read(10))
I heard th
#光标所在的位置,包括空格,在python3里,字符集为utf-8时,英文是1个字母1个字符,一个中文占3个字符,python2是中英一样
In [4]: print(f.tell())
10

  

7.seek调整光标 ****
使用场景:服务器客户端上传下载,如上传100M的文件,传到50M时断了,使用断点续传时,如果判断到它传了50M,可以使用seek调整到50M的位置

In [5]: f=open('/root/py/fullstack_s2/week2/day8/file.txt','r')
In [6]: print(f.tell())
0
In [7]: print(f.read(2))
I
In [8]: print(f.tell())
2
In [9]: f.seek(0)
Out[9]: 0
In [10]: print(f.tell())
0

  

8.文件操作之flush
场景分析:当f.write时是没有写到disk,直到f.close后才写到disk,是先写到缓存中,直到满了或者close,如果实时写到disk会影响性能,不过在数据安全性高的场景就需要实时写磁盘,使用flush方法来实现,当write一条数据后,使用flush就会把缓存更新,把数据存放到磁盘上去

In [3]: f=open('test','w')
In [4]: f.write('reid is good')
Out[4]: 12
In [5]: f.write('hello')
Out[5]: 5
[root@zabbix day8]# cat test ##write后并没有写到disk
[root@zabbix day8]#
In [6]: f.flush() ##flush后把缓存的数据写到磁盘上
[root@zabbix day8]# cat test
reid is goodhello[root@zabbix day8]#

flush在进度条的使用

In [7]: import sys,time
In [8]: for i in range(30):
...: sys.stdout.write("*") #sys.stdout终端的输出
...: time.sleep(0.2)
****************************** ##这种情况,它是先写到缓存里,当30个完成写完,0.2*30=6秒钟后才统一显示实现进度条:
In [9]: for i in range(30):
...: sys.stdout.write("*")
...: sys.stdout.flush() ##加上flush后,就会每0.4秒打印一个*显示在终端
...: time.sleep(0.4)
...:
******************************另一种方法:
In [13]: for i in range(30):
...: print('*',end='',flush=True) #end等于空不换行,flush作为print的参数立刻刷新
...: time.sleep(0.1)
******************************

9.truncate():截断,取决于光标所在的位置开始截取
追加的情况:

[root@zabbix day8]# cat file1.txt
hello world
In [22]: f=open('/root/py/fullstack_s2/week2/day8/file1.txt','a')
In [23]: f.truncate(4)
Out[23]: 4
[root@zabbix day8]# cat file1.txt
hell[root@zabbix day8]#

写的情况:

[root@zabbix day8]# cat file1.txt
hello world
In [24]: f=open('/root/py/fullstack_s2/week2/day8/file1.txt','w')
In [25]: f.truncate(4)
Out[25]: 4
[root@zabbix day8]# cat file1.txt
[root@zabbix day8]#
In [26]: f.write('hi reid') ##先写再截取
Out[26]: 7
In [27]: f.truncate(4)
Out[27]: 4
[root@zabbix day8]# cat file1.txt
hi r[root@zabbix day8]#

10.with方法
在退出with语句后,会自动把文件进行关闭,不使用f.close()

In [153]: with open('file1.txt','r') as f:
...: print(f.readline())
...:
I heard the echo

在python2.7后,with可以同时对两个不同的对象操作

In [154]: with open('file1.txt','r') as f_read, open('file2.txt','w') as f_write:
...: for line in f_read:
...: f_write.write(line) ##读file1.txt文件,把每行写入file2.txt中

四.修改文件的思路(*****)
注:r与w的光标是不一致的,r是从起始开始,w写是从最后开始写的
思路一:直接在光标所在的位置修改

[root@zabbix day8]# cat file1.txt
I heard the echo
Open to the lonely
Repeat outrightly
Eventually swaying
In [66]: f=open('/root/py/fullstack_s2/week2/day8/file1.txt','r+')
In [67]: num=0
In [68]: for line in f:
...: num+=1
...: if num==3: ###按理论当num==3时,是光标所在行,当时在最后写
...: f.write(' ++++reid')
...: f.close()
[root@zabbix day8]# cat file1.txt
I heard the echo
Open to the lonely
Repeat outrightly
Eventually swaying
++++reid[root@zabbix day8]#[root@zabbix day8]# cat file1.txt
I heard the echo
Open to the lonely
Repeat outrightly
Eventually swaying
In [69]: f=open('/root/py/fullstack_s2/week2/day8/file1.txt','r+')
In [70]: num=0
In [71]: for line in f:
...: num+=1
...: if num==1: ##当num==1时,还是在最后一行写
...: f.write(' ++++reid')
...: f.close()
[root@zabbix day8]# cat file1.txt
I heard the echo
Open to the lonely
Repeat outrightly
Eventually swaying
++++reid[root@zabbix day8]#

  

思路二:创建新的文件,把旧文件每个读到一个新文件里,读到某一处理的行时,再处理,新的文件就修改完
注意:
a. 调用read,write的方法时,要是对象,如file文件,字符串不行
b. 创建一个新对象时,不要放进for循环中,否则会产生多个对象

[root@zabbix day8]# cat file1.txt
I heard the echo
Open to the lonely
Repeat outrightly
Eventually swaying[root@zabbix day8]# cat test.py
#!/usr/local/python3/bin/python3
f=open('/root/py/fullstack_s2/week2/day8/file1.txt','r')         ##从一个文件里读
f1=open('/root/py/fullstack_s2/week2/day8/file2.txt','w')        ##写入一个新创建的文件
num=0
for line in f:num+=1if num==3:                                                   ##当光标到第三行时操作#line='hello reid \n'    line=' '.join([line.strip(),'hello reid \n'])    f1.write(line)
f.close()
f1.close()[root@zabbix day8]# python3 test.py
[root@zabbix day8]# cat file2.txt
I heard the echo
Open to the lonely
Repeat outrightly hello reid  ###
Eventually swaying

  

转载于:https://www.cnblogs.com/reid21/articles/8506712.html

Python攻克之路-文件操作相关推荐

  1. Python中的File(文件)操作

    Python中的File(文件)操作 针对磁盘中的文件的读写.文件I/O I 输入(input) O输出(Output) 文件操作步骤:1.打开文件 2.读写文件 3.关闭文件 写入文件的操作:(把大 ...

  2. 详解Python中的File(文件)操作

    目录 Python中的File(文件)操作 写入文件的操作: 读取文件的操作: 一.文件操作相关函数 1. open() 打开文件 2. seek() 设置文件指针的位置 3. write() 写入内 ...

  3. Python之路----文件操作

    文件操作 1.能调用方法的一定是对象,比如数值.字符串.列表.元组.字典,甚至文件也是对象,Python中一切皆为对象. 1 str1 = 'hello' 2 str2 = 'world' 3 str ...

  4. 【八】Python全栈之路--文件操作

    文章目录 1. 字典的相关函数 2. 集合的操作_函数 3. 文件基本操作 4. 文件扩展模式 1. 字典的相关函数 字典的相关函数 # ### 字典的相关函数 dic = {} # 增 # 1.普通 ...

  5. 第三章:Python基础の函数和文件操作实战

    本課主題 Set 集合和操作实战 函数介紹和操作实战 参数的深入介绍和操作实战 format 函数操作实战 lambda 表达式介绍 文件操作函数介紹和操作实战 本周作业 Set 集合和操作实战 Se ...

  6. Python 开发-2 (文件操作,正则表达式,类,JSON,装饰器,异常处理,Socket)

    文章目录 文件操作 递归创建目录 删除文件或目录 拷贝文件 修改文件名或目录名 文件路径名操作 判断文件,目录是否存在 文件大小与修改时间 当前目录与目录切换 遍历目录文件 遍历目录下指定后缀 目录文 ...

  7. python处理二进制文件_python文件操作之二进制

    列表项 三元运算符号: a=3 b=7 val=a if a>b else val=b print(val) 文件处理 首先给你一个文件,或者自己建立一个文件,那如何查看文件的内容呢? 1.安装 ...

  8. python codecs.open()及文件操作-文本处理 with open

    20210810 以二进制并追加的形式写入文件 20210731 f.write('\n') 换行写入 20210625 https://blog.csdn.net/a543402496/articl ...

  9. Python基本语法_文件操作_读写函数详解

    目录 目录 软件环境 file文件对象 open文件操作 读文件 read读取所有文件内容 readline获取一行内容 readlines读取所有文件内容 readreadlinereadlines ...

  10. 【python教程】IO文件操作的常见用法

    首先创建一个文件操作对象: f = open(file, mode, encoding) file指定文件的路径,可以是绝对路径,也可以是相对路径 文件的常见mode: mode = "r& ...

最新文章

  1. guava-cache设计与实现
  2. python itchat 的使用
  3. java8安装_JMeter必知必会系列(2) JMeter安装部署
  4. spring-boot-devtools
  5. python名片管理系统视频_python视频学习笔记6(名片管理系统开发)
  6. 百度大脑“乘风”新基建,“破浪”产业智能化落地
  7. 在Windows中测试c语言单个函数运行时间方法
  8. php本地文件包含漏洞,php文件包含漏洞利用小结
  9. windows server 2003 远程桌面最大连接数调整与windows 2008远程桌面后,本地帐号自动锁定...
  10. 开会坐在后面,意思是不感兴趣
  11. 注册测绘师的学习笔记
  12. Python游戏之运动物体寻路——A星算法与扩展
  13. 【P4lang】P4数据平面编程实验basic_tunnel
  14. 设计师需要的知识体系
  15. 电脑上玩手机游戏,效果秒杀模拟器
  16. 毕业设计-基于SSM框架大学教务管理平台项目开发实战教程(附源码)
  17. 网络爬虫requests和bs4简单入门
  18. js中的Boolean
  19. 基于 jsp+servlet+mysql 开发旅游推介网站和后台管理系统(maven项目)
  20. Java Web:开篇综述与第一章

热门文章

  1. 我国计算机应用行业现状,2020年计算机应用系统的前景趋势
  2. WebSocket前后端通信代码
  3. java 数据透视表 组件_Java 创建 Excel 数据透视表
  4. Qt去除未使用变量警报
  5. http://localhost:63342/健身房前端项目开发/Fitness_room/css/bootstrap.min.css.map?_ijt=9ldd0n7td4j0b0neg8f55qn
  6. 大一C语言程序设计练习题(四)
  7. 【问题导向】GEE+Arcgis绘制广西不同时期的水资源分布图
  8. 高校重金引进23名菲律宾博士,有关部门已展开调查!
  9. 微信与服务器通信失败,微信公众平台开发服务器与微信服务器通信时失败 获取不到值...
  10. Android碎片化Fragment实例一