一、文件操作
文件操作流程:
1.找到并打开一个文件,得到其文件句柄并赋值给一个变量
2.通过句柄对文件进行操作
3.关闭文件
文件示例
1 The way she came into the place 她走进来 2 I knew right then and there 所踩的步伐,那时那刻,即让我发觉 3 there was something different about this girl 这女孩儿与众不同 4 The way she moved 她的步调 5 her hair 她的秀发 6 her face 她的容颜 7 her lines 她优雅的线条 8 Divinity in motion 一举一动似有某种神力 9 As she stalked the room 她一踏进 10 I could feel the aura of her presence 我便察觉,她存在的气息 11 Every head turned feeling passion and lust 人人回首,妄想痴心 12 The girl was persuasive 这女孩儿太会蛊惑人心 13 the girl I could not trust 她,我是万不能相信 14 The girl was bad 这女孩儿让人神魂颠倒 15 the girl was dangerous 可是危险得很 16 I never know 我心知肚明 17 but I was walking in line 但我仍踏上了警戒线 18 Come go with me “跟我来吧” 19 I said I have no time 我说我没时间 20 She said :"Don‘t you pretend we didn‘t talk on the phone 她说:“我们不是曾通过电话,不要推托再三” 21 My baby cried 闻言,心伤泪下 22 she left me standing aloneMy baby 转身离去,留我一人呆站其间 23 She‘s so dangerous 太危险 24 the girl is so dangerous 这女孩实在危险 25 Take away my money, throw away my time 拿走我的钱,浪费我的时间 26 You can call me honey 你可以叫我Honey 27 but you‘re no damn good for me 但你对我一点儿该死的好处也没有 28 She came at me in sections with the eyes of desire 在街区,她向我走来,欲望烈焰眼中燃烧 29 I fell trapped into her web of sin 我落入她所织构的罪恶之网 30 A touch 一下轻抚 31 a kiss 一次热吻 32 a whisper of love 一声柔情蜜意的低语 33 I was at the point of no return 我迈向了不归路 34 deep in the darkness of passion‘s insanity 深陷于疯狂痴迷的黑暗之中 35 I felt taken by lust‘s strange inhumanity 被某种怪异而非理性的贪婪冲昏了头脑 36 This girl was persuasive 这女孩儿太会蛊惑人心 37 this girl I could not trust 她,我是万不能相信 38 The girl was bad, the girl was dangerous 这女孩儿让人神魂颠倒,可是危险得很 39 I never knew but I was living in vain 我从未想过,会落到那般境地 40 She called my house and she know my name 她来到我家中,并声称,她知道我的名字 41 And don‘t you pretend you never did me before “不要装做我们不曾在一起” 42 With tears in her eyes 眼中噙泪 43 my baby walked out the door,My baby 夺门离去 44 She‘s so dangerous 太危险 45 the girl is so dangerous 这女孩实在危险 46 Take away my money 拿走我的钱 47 throw away my time 浪费我的时间 48 You can call me honey 你可以叫我Honey 49 but you‘re no damn good for me 但你对我一点儿该死的好处也没有 50 Dangerous 危险 51 the girl is so dangerous 这女孩太危险 52 I have to pray to God 我只得祈祷上苍 53 ‘cause I know how lust can blind 因为我知道贪婪是如何地使人心智蒙蔽 54 It‘s a passion in my soul 是有种冲动,隐匿于我的灵魂中 55 But you‘re no damn lover /friend of mine 但你该死的决不是我的lover更不是我的朋友 56 I cannot sleep alone tonight 今晚我难以入眠 57 my baby left me here tonight 留我一人形孤影单 58 I cannot cope‘til it all right 我辗转反侧直到意识逐渐飘远 59 You and your manipulation 你喜欢别人受你摆布 60 you hurt my baby 你把我的 baby深深地伤害 61 And then it happened 就那么发生了 62 she touched me 她在我身上轻抚 63 For the lips of a strange woman drop as a honeycomb 这陌生的女人,覆上了如蜜的双唇 64 And her mouth was smoother than oil 比油还滑的嘴里 65 But her inner spirit and words were sharp as a two-edged sword 吐出的言词,以及她的内在心灵,却锋利得似双刃剑 66 But I loved it ‘cause it‘s dangerous 但是恰恰因为险象环生,我喜欢 67 She‘s so dangerous, the girl is so dangerous 太危险,这女孩实在危险 68 Take away my money, throw away my time 拿走我的钱,浪费我的时间 69 You can call me honey 你可以叫我Honey 70 but you‘re no damn good for me 但你对我是一点儿该死的好处也没有 71 Dangerous 危险 72 the girl is so dangerous 这女孩太危险 73 I have to pray to God 我只得祈祷上苍 74 ‘cause I know how lust can blind 因为我知道贪婪如何使人心智蒙蔽 75 It‘s a passion in my soul 灵魂中是有种冲动 76 But you‘re no damn lover /friend of mine 但你该死的决不是我的lover更不是我的朋友 77 She‘s so dangerous 太危险 78 the girl is so dangerous 这女孩实在危险
常用操作
读
1 f = open("dangerous",encoding = ‘utf-8‘) #默认为读,文件句柄,包含文件名,大小,内存中起始位置等
2 data = f.read() #读取整个文件,读完之后句柄到最后
3 data2 = f.read() #data2为空
4 print(data)
5 print(‘data2‘,data2)
1 The way she came into the place 她走进来 2 I knew right then and there 所踩的步伐,那时那刻,即让我发觉 3 there was something different about this girl 这女孩儿与众不同 4 The way she moved 她的步调 5 her hair 她的秀发 6 her face 她的容颜 7 her lines 她优雅的线条 8 Divinity in motion 一举一动似有某种神力 9 As she stalked the room 她一踏进 10 I could feel the aura of her presence 我便察觉,她存在的气息 11 Every head turned feeling passion and lust 人人回首,妄想痴心 12 The girl was persuasive 这女孩儿太会蛊惑人心 13 the girl I could not trust 她,我是万不能相信 14 The girl was bad 这女孩儿让人神魂颠倒 15 the girl was dangerous 可是危险得很 16 I never know 我心知肚明 17 but I was walking in line 但我仍踏上了警戒线 18 Come go with me “跟我来吧” 19 I said I have no time 我说我没时间 20 She said :"Don‘t you pretend we didn‘t talk on the phone 她说:“我们不是曾通过电话,不要推托再三” 21 My baby cried 闻言,心伤泪下 22 she left me standing aloneMy baby 转身离去,留我一人呆站其间 23 She‘s so dangerous 太危险 24 the girl is so dangerous 这女孩实在危险 25 Take away my money, throw away my time 拿走我的钱,浪费我的时间 26 You can call me honey 你可以叫我Honey 27 but you‘re no damn good for me 但你对我一点儿该死的好处也没有 28 She came at me in sections with the eyes of desire 在街区,她向我走来,欲望烈焰眼中燃烧 29 I fell trapped into her web of sin 我落入她所织构的罪恶之网 30 A touch 一下轻抚 31 a kiss 一次热吻 32 a whisper of love 一声柔情蜜意的低语 33 I was at the point of no return 我迈向了不归路 34 deep in the darkness of passion‘s insanity 深陷于疯狂痴迷的黑暗之中 35 I felt taken by lust‘s strange inhumanity 被某种怪异而非理性的贪婪冲昏了头脑 36 This girl was persuasive 这女孩儿太会蛊惑人心 37 this girl I could not trust 她,我是万不能相信 38 The girl was bad, the girl was dangerous 这女孩儿让人神魂颠倒,可是危险得很 39 I never knew but I was living in vain 我从未想过,会落到那般境地 40 She called my house and she know my name 她来到我家中,并声称,她知道我的名字 41 And don‘t you pretend you never did me before “不要装做我们不曾在一起” 42 With tears in her eyes 眼中噙泪 43 my baby walked out the door,My baby 夺门离去 44 She‘s so dangerous 太危险 45 the girl is so dangerous 这女孩实在危险 46 Take away my money 拿走我的钱 47 throw away my time 浪费我的时间 48 You can call me honey 你可以叫我Honey 49 but you‘re no damn good for me 但你对我一点儿该死的好处也没有 50 Dangerous 危险 51 the girl is so dangerous 这女孩太危险 52 I have to pray to God 我只得祈祷上苍 53 ‘cause I know how lust can blind 因为我知道贪婪是如何地使人心智蒙蔽 54 It‘s a passion in my soul 是有种冲动,隐匿于我的灵魂中 55 But you‘re no damn lover /friend of mine 但你该死的决不是我的lover更不是我的朋友 56 I cannot sleep alone tonight 今晚我难以入眠 57 my baby left me here tonight 留我一人形孤影单 58 I cannot cope‘til it all right 我辗转反侧直到意识逐渐飘远 59 You and your manipulation 你喜欢别人受你摆布 60 you hurt my baby 你把我的 baby深深地伤害 61 And then it happened 就那么发生了 62 she touched me 她在我身上轻抚 63 For the lips of a strange woman drop as a honeycomb 这陌生的女人,覆上了如蜜的双唇 64 And her mouth was smoother than oil 比油还滑的嘴里 65 But her inner spirit and words were sharp as a two-edged sword 吐出的言词,以及她的内在心灵,却锋利得似双刃剑 66 But I loved it ‘cause it‘s dangerous 但是恰恰因为险象环生,我喜欢 67 She‘s so dangerous, the girl is so dangerous 太危险,这女孩实在危险 68 Take away my money, throw away my time 拿走我的钱,浪费我的时间 69 You can call me honey 你可以叫我Honey 70 but you‘re no damn good for me 但你对我是一点儿该死的好处也没有 71 Dangerous 危险 72 the girl is so dangerous 这女孩太危险 73 I have to pray to God 我只得祈祷上苍 74 ‘cause I know how lust can blind 因为我知道贪婪如何使人心智蒙蔽 75 It‘s a passion in my soul 灵魂中是有种冲动 76 But you‘re no damn lover /friend of mine 但你该死的决不是我的lover更不是我的朋友 77 She‘s so dangerous 太危险 78 the girl is so dangerous 这女孩实在危险 79 data2
写
1 f = open("dangerous2",‘w‘,encoding = ‘utf-8‘) #写的时候是重新创建一个文件,覆盖原文件再写入 2 f.write("我爱北京天安门\n") 3 f.write("天安门上太阳↑")
1 #dangerous2 2 我爱北京天安门 3 天安门上太阳↑
追加
1 f = open("dangerous2",‘a‘,encoding = ‘utf-8‘) # a = append 2 f.write("\n我爱北京天安门。。。。。。。。。。。。。。\n") 3 f.write("天安门上太阳升。。。")
1 #dangerous2 2 我爱北京天安门 3 天安门上太阳↑ 4 我爱北京天安门。。。。。。。。。。。。。。 5 天安门上太阳升。。。
按行读
1 f = open("dangerous2",‘r‘,encoding = ‘utf-8‘) 2 print(f.readline())
The way she came into the place 她走进来
利用循环按行读取整个文件
1 f = open("dangerous",‘r‘,encoding = ‘utf-8‘) 2 for line in f.readlines(): 3 print(line.strip())
1 #不输出第十行 low 读出的内容一直在内存中 2 f = open("dangerous",‘r‘,encoding = ‘utf-8‘) 3 for index,line in enumerate(f.readlines()): 4 if index == 9 : 5 print(‘我是分割线‘.center(50,‘-‘)) 6 continue 7 print(line.strip())
1 #不输出第十行 high 内存中只存一行 2 f = open("dangerous",encoding = ‘utf-8‘) 3 count = 0 4 for line in f: 5 if count == 9: 6 print(‘我是分割线‘.center(50,‘-‘)) 7 count += 1 8 continue 9 print(line.strip()) 10 count += 1
1 The way she came into the place 她走进来 2 I knew right then and there 所踩的步伐,那时那刻,即让我发觉 3 there was something different about this girl 这女孩儿与众不同 4 The way she moved 她的步调 5 her hair 她的秀发 6 her face 她的容颜 7 her lines 她优雅的线条 8 Divinity in motion 一举一动似有某种神力 9 As she stalked the room 她一踏进 10 ----------------------我是分割线----------------------- 11 Every head turned feeling passion and lust 人人回首,妄想痴心 12 The girl was persuasive 这女孩儿太会蛊惑人心 13 the girl I could not trust 她,我是万不能相信 14 The girl was bad 这女孩儿让人神魂颠倒 15 the girl was dangerous 可是危险得很 16 I never know 我心知肚明 17 but I was walking in line 但我仍踏上了警戒线 18 Come go with me “跟我来吧” 19 I said I have no time 我说我没时间 20 She said :"Don‘t you pretend we didn‘t talk on the phone 她说:“我们不是曾通过电话,不要推托再三” 21 My baby cried 闻言,心伤泪下 22 she left me standing aloneMy baby 转身离去,留我一人呆站其间 23 She‘s so dangerous 太危险 24 the girl is so dangerous 这女孩实在危险 25 Take away my money, throw away my time 拿走我的钱,浪费我的时间 26 You can call me honey 你可以叫我Honey 27 but you‘re no damn good for me 但你对我一点儿该死的好处也没有 28 She came at me in sections with the eyes of desire 在街区,她向我走来,欲望烈焰眼中燃烧 29 I fell trapped into her web of sin 我落入她所织构的罪恶之网 30 A touch 一下轻抚 31 a kiss 一次热吻 32 a whisper of love 一声柔情蜜意的低语 33 I was at the point of no return 我迈向了不归路 34 deep in the darkness of passion‘s insanity 深陷于疯狂痴迷的黑暗之中 35 I felt taken by lust‘s strange inhumanity 被某种怪异而非理性的贪婪冲昏了头脑 36 This girl was persuasive 这女孩儿太会蛊惑人心 37 this girl I could not trust 她,我是万不能相信 38 The girl was bad, the girl was dangerous 这女孩儿让人神魂颠倒,可是危险得很 39 I never knew but I was living in vain 我从未想过,会落到那般境地 40 She called my house and she know my name 她来到我家中,并声称,她知道我的名字 41 And don‘t you pretend you never did me before “不要装做我们不曾在一起” 42 With tears in her eyes 眼中噙泪 43 my baby walked out the door,My baby 夺门离去 44 She‘s so dangerous 太危险 45 the girl is so dangerous 这女孩实在危险 46 Take away my money 拿走我的钱 47 throw away my time 浪费我的时间 48 You can call me honey 你可以叫我Honey 49 but you‘re no damn good for me 但你对我一点儿该死的好处也没有 50 Dangerous 危险 51 the girl is so dangerous 这女孩太危险 52 I have to pray to God 我只得祈祷上苍 53 ‘cause I know how lust can blind 因为我知道贪婪是如何地使人心智蒙蔽 54 It‘s a passion in my soul 是有种冲动,隐匿于我的灵魂中 55 But you‘re no damn lover /friend of mine 但你该死的决不是我的lover更不是我的朋友 56 I cannot sleep alone tonight 今晚我难以入眠 57 my baby left me here tonight 留我一人形孤影单 58 I cannot cope‘til it all right 我辗转反侧直到意识逐渐飘远 59 You and your manipulation 你喜欢别人受你摆布 60 you hurt my baby 你把我的 baby深深地伤害 61 And then it happened 就那么发生了 62 she touched me 她在我身上轻抚 63 For the lips of a strange woman drop as a honeycomb 这陌生的女人,覆上了如蜜的双唇 64 And her mouth was smoother than oil 比油还滑的嘴里 65 But her inner spirit and words were sharp as a two-edged sword 吐出的言词,以及她的内在心灵,却锋利得似双刃剑 66 But I loved it ‘cause it‘s dangerous 但是恰恰因为险象环生,我喜欢 67 She‘s so dangerous, the girl is so dangerous 太危险,这女孩实在危险 68 Take away my money, throw away my time 拿走我的钱,浪费我的时间 69 You can call me honey 你可以叫我Honey 70 but you‘re no damn good for me 但你对我是一点儿该死的好处也没有 71 Dangerous 危险 72 the girl is so dangerous 这女孩太危险 73 I have to pray to God 我只得祈祷上苍 74 ‘cause I know how lust can blind 因为我知道贪婪如何使人心智蒙蔽 75 It‘s a passion in my soul 灵魂中是有种冲动 76 But you‘re no damn lover /friend of mine 但你该死的决不是我的lover更不是我的朋友 77 She‘s so dangerous 太危险 78 the girl is so dangerous 这女孩实在危险
文件句柄
1 f = open("dangerous",encoding = ‘utf-8‘) #文件句柄,包含文件名,大小,内存中起始位置等 2 print(f.tell()) #获取句柄位置 3 print(f.readline()) 4 print(f.tell()) #句柄位置按字符计数 5 f.seek(10) #回到句柄位置0 6 print(f.readline())
1 0 2 The way she came into the place 她走进来 3 4 46 5 e came into the place 她走进来
截断
1 f = open("dangerous",‘a‘,encoding = ‘utf-8‘) 2 f.truncate(5)
#dangerous The w
强制刷新
1 #进度条 f.flush()与其功能相同 2 import sys,time 3 4 for i in range(50): 5 sys.stdout.write(‘%d ‘%(i)) 6 sys.stdout.flush() 7 time.sleep(0.1)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
其他操作
1 f = open("dangerous",encoding = ‘utf-8‘) 2 print(f.encoding) #打印文件编码 3 print(f.fileno()) #打印文件句柄编号 4 print(f.name) #打印文件名 5 print(f.isatty()) #判断是否为终端设备 6 print(f.seekable()) #判断句柄是否能移 7 print(f.readable()) #判断文件是否可读 8 print(f.writable()) #判断文件是否可写
1 utf-8 2 3 3 dangerous 4 False 5 True 6 True 7 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
通过将原文件写到一个新文件中来更改文件
1 f = open(‘dangerous‘,‘r‘,encoding=‘utf-8‘) 2 f_new = open(‘dangerous_new‘,‘w‘,encoding=‘utf-8‘) 3 for line in f: 4 if "女孩儿" in line: 5 line = line.replace(‘女孩‘,‘zz‘) 6 f_new.write(line) 7 f.close() 8 f_new.close()
with语句
为了避免文件打开后忘记关闭,可采用with语句
1 with open(‘dangerous‘,‘r‘,encoding=‘utf-8‘) as f: 2 for line in f: 3 print(line)
with语句运行结束后,自动关闭文件。
在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:
1 with open(‘log1‘) as obj1, open(‘log2‘) as obj2: 2 ...