python基础2_2-文件操作

1、文件基本操作

流程:

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

基本操作

#基本操作
f = open('test.txt', 'w', encoding='utf-8') # open file

first_line = f.readline() #光标移到下一行
print(first_line) # 读一行

data = f.read() # 读取剩下的所有内容,文件大时不要用
print(data) # 打印文件

f.write("hello")
f.encoding # 文件编码类型
文件读写中有一个内存缓存机制,例如写内容到文件时,写一行,会先存储在内存缓存中,等到缓存满了,一次性写入硬盘中
f.flush() # 将内存中的东西刷新写入到硬盘中
f.turncate() # 清空文件
f.turncate(10) # 截断文件,从头开始留10个

# low loop
for line in f.readlines(): # deadlines()将所有内容保存到内存中
	print(line.strip())

for index, line in enumerate(f.readlines()):
	if line == 9:
		continue
	print(line.strip()) # 去空格回车

# high loop	
for line in f: # 读一行,保存一行在内存中
	print(line)

f.tell() # 光标,查看光标位置
f.seek(0) # 光标回去

f.close() # 关闭文件

打开文件的模式有:
在这里插入图片描述

with语句
为了避免打开文件后忘记关闭,可以通过管理上下文,即:

with open('log', 'r') as f:
	pass

如此方式,在with代码块执行完毕时,内部会自动关闭和释放文件资源。
在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

with open('log1') as obj1, open('log2') as obj2:
	pass

2、文件修改方式

1、整个读取到内存里,修改完成,写回原文件中。

2、打开一个文件,一行一行读取,找到需要修改的地方,写入到新文件里去。

现有文件yesterday如下

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
当我年少轻狂

文件修改.py

# Author: Severen

f = open('yesterday', 'r', encoding="utf-8")
f_new = open('yesterday.bak', 'w', encoding="utf-8")

for line in f:
    if "肆意的快乐等我享受" in line:
        line = line.replace("肆意的快乐等我享受", '肆意的快乐等severen享受')
    f_new.write(line)p
    
f.close()
f_new.close()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值