当前位置:首页 > 编程笔记 > 正文
已解决

第7章 文件读取操作

来自网友在路上 179879提问 提问时间:2023-11-08 14:24:40阅读次数: 79

最佳答案 问答题库798位专家为你答疑解惑

7.5 用python操作文件的3种模式

在这里插入图片描述
读的模式打开只能读,写模式打开只能写。
类似于word的只读模式
所以python打开文件得🔝文件

文件打开模式(文本模式)
在这里插入图片描述

[DEV (v.v) sa_cluster@hybrid01 ~]$ cat test.py 
f = open("name_list",mode="w")f.write("张三")
f.write("李四")f.close()[DEV (v.v) sa_cluster@hybrid01 ~]$ cat name_list 
张三李四[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$ ##文件已经存在就会覆盖
[DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
[DEV (v.v) sa_cluster@hybrid01 ~]$ cat name_list 
张三
李四
[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$  cat test.py 
f = open("name_list",mode="w")f.write("张三\n")
f.write("李四\n")## 文件只能写不能读,所以读会报错
DEV (v.v) sa_cluster@hybrid01 ~]$  cat test.py 
f = open("name_list",mode="w")f.write("张三\n")
f.write("李四\n")
f.read()         ##添加了这行
f.close()[DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
Traceback (most recent call last):File "/home/sa_cluster/test.py", line 5, in <module>f.read()
io.UnsupportedOperation: not readable####同理,读模式不能写
[DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
Traceback (most recent call last):File "/home/sa_cluster/test.py", line 3, in <module>f.write("张三\n")
io.UnsupportedOperation: not writable
[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$  cat test.py 
f = open("name_list",mode="r")f.write("张三\n")
f.write("李四\n")
f.read()
f.close()[DEV (v.v) sa_cluster@hybrid01 ~]$ ## 正常读,打印
DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
张三
李四[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$ cat test.py 
f = open("name_list",mode="r")# f.write("张三\n")
# f.write("李四\n")
print(f.read())
f.close()[DEV (v.v) sa_cluster@hybrid01 ~]$ ## 只想读部分
比如只想读第一行[DEV (v.v) sa_cluster@hybrid01 ~]$ cat test.py 
f = open("name_list",mode="r")# f.write("张三\n")
# f.write("李四\n")
print(f.read())
print('-------------')
print(f.readline())      ##光标已经到最后一行了,所以这里为空行
[DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
张三
李四-------------
[DEV (v.v) sa_cluster@hybrid01 ~]$ cat test.py 
f = open("name_list",mode="r")print(f.readline())
print('-------------')
print(f.read())[DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
张三##这一行是因为readline自己带了换行符,然后print打印出来了
-------------
李四

追加模式
在这里插入图片描述
追加的时候也是只能写,不能读,读会报错
追加就是写日志的时候比较适合使用。

7.6 遍历文件

DEV (v.v) sa_cluster@hybrid01 ~]$ cat name_list 
张三  深圳  173  48        ##其实每一行最后都有一个\n
李四  北京  182  32
王武  南京  178  64
收到  上海  163  50
发到  西安  183  32
地方  湖北  123  77
如果  宁夏  170  65[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
张三  深圳  173  48 李四  北京  182  32王武  南京  178  64收到  上海  163  50发到  西安  183  32地方  湖北  123  77如果  宁夏  170  65
[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$ cat test.py 
f = open('name_list')
for lie in f:print(lie)         [DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
['张三  深圳  173  48 \n', '李四  北京  182  32\n', '王武  南京  178  64\n', '收到  上海  163  50\n', '发到  西安  183  32\n', '地方  湖北  123  77\n', '如果  宁夏  170  65']
[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$ cat test.py 
f = open('name_list')
print(f.readlines())
# for lie in f:
#     print(lie)[DEV (v.v) sa_cluster@hybrid01 ~]$ 

分成列表

[DEV (v.v) sa_cluster@hybrid01 ~]$ cat test.py 
f = open('name_list')
for lie in f:lie = lie.split()print(lie)[DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
['张三', '深圳', '173', '48']
['李四', '北京', '182', '32']
['王武', '南京', '178', '64']
['收到', '上海', '163', '50']
['发到', '西安', '183', '32']
['地方', '湖北', '123', '77']
['如果', '宁夏', '170', '65']###拿三四列
DEV (v.v) sa_cluster@hybrid01 ~]$ cat test.py 
f = open('name_list')
for lie in f:lie = lie.split()# print(lie)heigt = lie[2]weight= lie[3]if int(heigt) >= 170 and int(weight) <= 50:print(lie)[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$ 
[DEV (v.v) sa_cluster@hybrid01 ~]$ python3 test.py 
['张三', '深圳', '173', '48']
['李四', '北京', '182', '32']
['发到', '西安', '183', '32']
查看全文

99%的人还看了

相似问题

猜你感兴趣

版权申明

本文"第7章 文件读取操作":http://eshow365.cn/6-35394-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!