首页 > 编程语言 > 详细

python-文件读写

时间:2019-12-03 18:24:36      阅读:76      评论:0      收藏:0      [点我收藏+]

对文件操作流程
1.打开文件,得到文件句柄并赋值给一个变量
2.通过句柄对文件进行操作
3.关闭文件

# data = open("test_file").read()
data = open("test_file",encoding="UTF-8").read() #有的操作系统编码格式是gdk,python3是UTF-8,读取不了,所以?encoding默认读取格式为UTF-8
print(data)

以下面这个文件来进行读取

技术分享图片

只读

f = open("test_file",encoding="UTF-8")  #文件句柄,就是这个文件的内存对象,包含文件的名字、字符集、大小等
data = f.read()
data2 = f.read()
print(data)
print(‘data2‘,data2) #data2读不到,data读到最后一行,data2是继续读,所以读不到。文件一遍读完就没了

只写

f = open("test_file2",‘w‘,encoding="UTF-8")  #没有写,默认就是r,读模式;w写,是创建一个文件,会覆盖之前的,这个只能写,不能读;
f.write("桃之夭夭,灼灼其华。之子于归,宜其室家。\n")
f.write("桃之夭夭,有蕡其实。之子于归,宜其家室。")

追加

f = open("test_file2",‘a‘,encoding="UTF-8")  #a往后追加,不覆盖原来的文件,也不能读;
f.write("\n桃之夭夭,其叶蓁蓁。之子于归,宜其家人。")
f.close()
f = open("test_file2",‘r‘,encoding="UTF-8")
print(f.readline()) #读一行
#读多行
for i in range(5):
f = open("test_file2",‘r‘,encoding="UTF-8")
# print(f.readlines()) #读出来是个列表,每行一个元素
for line in f.readlines():
print(line.strip()) #默认每行后面都有一个换行符,strip去掉空格和换行
f = open("test_file",‘r‘,encoding="UTF-8")
#第10行不打印
#low loop
for index,line in enumerate(f.readlines()): #这个只适合文件不大的情况
if index == 9:
print("------分割线-------")
continue
print(line.strip())
f = open("test_file",‘r‘,encoding="UTF-8")
#高效的循环方法
count = 0
for line in f: #一行一行读,内存每次只存一行
if count == 9:
print("-------分割线------")
count += 1
continue
print(line)
count += 1
f.close()
f = open("test_file", ‘r‘)
print(f.tell()) # 指针所在位置
print(f.readline())
print(f.tell()) # tell是按字符计数的
f.seek(0) # 指针回到0
print(f.encoding) # 查看文件编码
print(f.name) # 查看文件名字
print(f.readable()) # 判断文件是否可读
print(f.writable()) # 判断文件是否可写
print(f.closed) # 判断文件是否关闭,返回true/false

yayadeMac:~ ddc-test$ python3
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("/Users/ddc-test/Downloads/pycharm/Ex_user/test_file2","w")
>>> f.write("hello1\n")         #写入之后,打开文件并没有
7
>>> f.flush()     #打开之后有,实时刷新
>>>

以进度条为例:

import sys,time
for i in range(20):
sys.stdout.write("#")
sys.stdout.flush() #如果不写这一行就是等缓存区满了一下刷出来
time.sleep(0.1)
f = open("test_file2",‘a‘,encoding="UTF-8")
# f.truncate() #截断,不写会清空
f.seek(15)
f.truncate(21) #从头开始截断,移动光标不好使

读写

f = open("test_file2",‘r+‘)
print(f.readline())
f.write("-----------") #依然写在了最后,读和追加的功能
print(f.readline())

写读

f = open("test_file2",‘w+‘)   #先创建一个文件
print(f.readline()) #初始空
f.write("-----1------\n")
f.write("-----2------\n")
f.write("-----3------\n")
f.write("-----4------\n")
f.seek(10)
print(f.readline())
f.write("hhhhhhhhh") #依然在最后追加

追加读写

f = open("test_file2",‘a+‘)

二进制

文件是以二进制编码的

f = open("test_file2",‘rb‘)   #二进制文件,以二进制格式读,这时不能传encoding
f = open("test_file2",‘wb‘)
f.write("hello binary\n".encode())
f = open("test_file2",‘ab‘)

文件修改

f = open("test_file2",‘r‘)
f_new = open("test_file2.bak",‘w‘)
for line in f:
if "桃之夭夭" in line:
line = line.replace("桃之夭夭","替换内容")
f_new.write(line)
f.close()
f_new.close()

with语句

为了避免打开文件后忘记关闭,可以通过管理上下文,即
with open(‘log‘,‘r‘) as f:
  ...
如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。
支持同时对多个文件的上下文进行管理,即:
with open(‘log1‘,‘r‘) as obj1,open(‘log2‘,‘r‘) as obj2:
  Pass

with open("test_file2",‘r‘) as f:
print(f.readline())
#with这段语句执行完毕后自动关闭文件
with open("test_file1",‘r‘) as f1,\
open("test_file2", ‘r‘) as f2:

 

python-文件读写

原文:https://www.cnblogs.com/peiya/p/11978093.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!