ab File.flush() 概述:用来刷新缓冲区的,即将缓冲区的数据立刻写入文件,同时清空缓冲区,不需要是被动的等待输出缓冲区写入。
一般情况下文件关闭后会自动刷新缓冲区,但有时你需要在关闭前刷新她,这时就可以使用flush方法。
f=open('so_file','wb') #打开文件,并读取。 # data=f.read() #将读取的内容赋值给data # print(data)
print("file name is :",f.name)
f2=f.flush() # print(f2)
f.close() 运行结果:
file name is : so_file 注意:二进制模式下,不支持编码参数 缓冲flush作用效果图:(执行有惊喜) import sys,time
f=open('so_file','r+',encoding="utf-8") for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.1) 运行结果:
#################### File.fileno() 概述:返回一个整型的文件描述(file descriptor FD整型),可以用在如OS模块的read方法等一些底层操作上
f=open('so_file','wb') #打开文件,并读取。 # data=f.read() #将读取的内容赋值给data # print(data)
print("file name is :",f.name) # f2=f.flush()
fidd=f.fileno()
print("file》》》",fidd) #返回文件描述符
f.close() 运行结果:
file name is : so_file
file》》》 3 File.isatty() 概述:如果文件连接到一个终端上返回True,否则False
f=open('so_file','wb') #打开文件,并读取。 # data=f.read() #将读取的内容赋值给data # print(data)
print("file name is :",f.name)
fin=f.isatty()
print("file》》》",fin)
f.close()
运行结果:
file name is : so_file
file》》》 False File.readline() 概述:读取整行,包括\n字符
f=open('so_file','r+',encoding="utf-8")
print("file name is :",f.name) # for index in range(5): # line = next(fo) # print ("第 %d 行 - %s" % (index, line))
data=f.readline() #不填默认读取一行
print("读取首行:",data)
data1=f.readline(5) #读取N个字符
print("读取%s个字符" %(data1),data1)
f.close() 运行结果:
file name is : so_file
读取首行: Somehow, it seems the love I knew was always the most destructive kind
读取Yeste个字符 Yeste File.readlines() 概述:用于读取所有行(直到结束符 EOF)并返回列表,该列表可以由 Python 的 for... in ... 结构进行处理。 如果碰到结束符 EOF 则返回空字符串。
如果碰到结束符 EOF 则返回空字符串。
f=open('so_file','r+',encoding="utf-8")
print("file name is :",f.name)
data=f.readlines() #全部打印结尾加\n
print(data)
f.close() 第十行不打印
f=open('so_file','r+',encoding="utf-8") for index,info in enumerate(f.readlines()): if index==9:
print('--------GO-------') continue
print(info.strip())
f.close() 高效的循环方法:
f=open('so_file','r+',encoding="utf-8")
count=0 for line in f: if count==9:
print('---------GO--------')
count +=1 continue
print(line)
count +=1 File.tell() 概述:反馈文件当前位置,即文件指针当前位置
f=open('so_file','r+',encoding="utf-8")
data=f.readline()
print("读取数据为:%s"%(data))
data2=f.tell()
print("当前位置:%s" %(data2)) 运行结果:
读取数据为:Somehow, it seems the love I knew was always the most destructive kind
当前位置:72 #read结束,全文72行 File.seek() 概述:指针移动到之指定位置 File.truncate()
概述:用于从文件的首行首字符开始截断,截断文件为> .truncate()什么都不写,清空文件。指定数字就会截断个数。 File.write()
修改部分值,写入新文件。
f=open('so_file','r',encoding="utf-8")
f_new=open('so_file2','w',encoding="utf-8") for line in f: if "生命的滋味是甜的" in line:
line=line.replace("生命的滋味是甜的","生命的滋味是幸福的")
f_new.write(line)
f.close()
f_new.close() 运行结果: So_file文件内容
Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
The way the evening breeze
就如夜晚的微风
May tease the candle flame So_file2文件内容(新文件)
Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是幸福的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
The way the evening breeze
就如夜晚的微风
May tease the candle flame