对文件操作流程
- 打开文件,得到文件句柄并赋值给一个变量
- 通过句柄对文件进行操作
- 关闭文件
有以下文件:
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逗弄蜡烛的火苗The thousand dreams I dreamed我曾千万次梦见The splendid things I planned那些我计划的绚丽蓝图I always built to last on weak and shifting sand但我总是将之建筑在易逝的流沙上I lived by night and shunned the naked light of day我夜夜笙歌 逃避白昼赤裸的阳光And only now I see how the time ran away事到如今我才看清岁月是如何匆匆流逝Yesterday when I was young昨日当我年少轻狂So many lovely songs were waiting to be sung有那么多甜美的曲儿等我歌唱So many wild pleasures lay in store for me有那么多肆意的快乐等我享受And so much pain my eyes refused to see还有那么多痛苦 我的双眼却视而不见I ran so fast that time and youth at last ran out我飞快地奔走 最终时光与青春消逝殆尽I never stopped to think what life was all about我从未停下脚步去思考生命的意义And every conversation that I can now recall如今回想起的所有对话Concerned itself with me and nothing else at all除了和我相关的 什么都记不得了The game of love I played with arrogance and pride我用自负和傲慢玩着爱情的游戏And every flame I lit too quickly, quickly died所有我点燃的火焰都熄灭得太快The friends I made all somehow seemed to slip away所有我交的朋友似乎都不知不觉地离开了And only now I'm left alone to end the play, yeah只剩我一个人在台上来结束这场闹剧Oh, yesterday when I was young噢 昨日当我年少轻狂So many, many songs were waiting to be sung有那么那么多甜美的曲儿等我歌唱So many wild pleasures lay in store for me有那么多肆意的快乐等我享受And so much pain my eyes refused to see还有那么多痛苦 我的双眼却视而不见There are so many songs in me that won't be sung我有太多歌曲永远不会被唱起I feel the bitter taste of tears upon my tongue我尝到了舌尖泪水的苦涩滋味The time has come for me to pay for yesterday终于到了付出代价的时间 为了昨日When I was young当我年少轻狂
基本操作:
f = open('lyrics','w','encoding=utf-8') #打开文件 f为文件句柄data = f.read() #读文件print(data)f.close() #关闭文件 f.readable() #判断文件是否可读 f.writable() #判断文件是都可写 f.closed() #判断文件是否关闭 返回True 和 False
打开文件的模式有:
- r, 只读模式(默认)。
- w,只写模式。【不可读;不存在则创建;存在则删除内容;】
- a,追加模式。【可读; 不存在则创建;存在则只追加内容;】
"+" 表示可以同时读写某个文件
- r+, 可读写文件。【可读;可写;可追加】
- w+,写读 读写 先后顺序不一样 没什么卵用
- a+,同a
"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)
- rU
- r+U
"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)
- rb
- wb
- ab
readline() 一行一行的读
f = open('lyrics','r+',encoding='utf-8')print(f.readline()) #一行一行的读
readlines() 把读到的文件每一行 变成一个列表元素
f = open('lyrics','r+',encoding='utf-8')print(f.readlines()) 输出:['Somehow, it seems the love I knew was always the most destructive kind\n', 。。。。]
循环:读到哪一行就读到哪一行
f = open('lyrics','r+',encoding='utf-8')for index,line in enumerate(f.readlines()): if index ==9: print(-------分隔符------) #第九行打印分隔符 跳出本次循环 continue print(line)
但是这种循环效率不高,要先把文本转换成列表,如果文件太大,循环会死机
效率高的循环
count=0for line in f: if count==9: print("----------分隔符----------") count+=1 continue print(line) count+=1
tell()查看文件句柄指针的位置
f = open('lyrics','r+',encoding='utf-8')print(f.tell())f.read(16)print(f.tell())输出:016
seek() 移动文件句柄指针位置
f = open('lyrics','r+',encoding='utf-8')print(f.tell())f.read(16)print(f.tell())f.seek(0) #移动到文件字符行首print(f.tell())输出:0160
encoding() 打印文件的编码
f = open('lyrics','r+',encoding='utf-8')print(f.encoding())输出:utf-8
flush() 强制刷新到硬盘
f = open('lyrics','r+',encoding='utf-8')f.flush()
打印进度条
# -*-coding:utf-8-*-# Author:sunhaoimport sys,timefor i in range(10): sys.stdout.write('#') sys.stdout.flush() 如果不加flush 是等缓存满了之后一次性打印出来 加了之后是打印在一次强制刷新一次 time.sleep(1)
truncate() 截断
如果不写参数,清空文件