python_32_文件操作1


#目录里先创建一个yesterday文件
'''对文件操作流程:
打开文件,得到文件句柄并赋值给一个变量
通过句柄对文件进行操作
关闭文件 '''
print(open('yesterday',encoding='utf-8').read())#utf-8可以换成utf_8
print("\n>>>>>>>>>>>>>>>>>\n>>>>>>>>>>>>>>>>>\n" )

f = open('yesterday',encoding='utf-8') #打开文件(重新打开的,上一步打开过文件)
f = open('yesterday','r',encoding='utf-8')#打开文件,上一步是默认情况下打开读文件,标准是本步骤所写
data=f.read()
data2=f.read()
print(data)#读完之后,光标已在文件最后
print("\n*****************\n*****************\n" )
print(data2)#data2并未被打印出,说明文件只打印了一遍,因为上次读完之后,光标已在文件最后,无法继续读下去
#写文件,即创建新文件
f = open('yesterday2','w',encoding='utf-8')#打开写文件
#注:不能这样f = open('yesterday','w',encoding='utf-8')打开文件,会直接导致yesterday文件变成空文件
f.write("我爱北京天安门,\n")
f.write("这里靠近故宫")
f.close() #关闭文件
#写文件,但是不会覆盖之前的,也不能读出来
f = open('yesterday2','a',encoding='utf-8')#a=apeend 追加
f.write("\n我在天津上学,\n")
f.write("这里有红桥区,北辰区")
data3=f.read()#还是读不出来
print("data3没有被打印出来",data3)
#data = f.read()  读取剩下的所有内容,文件大时不要用读取剩下的所有内容,文件大时不要用

  


我的文件,保存到目录中
1 YESTERDAY ONCE MORE
2 When I was young I'd listen to the radio 当年少时,我爱听收音机
3 Waiting for my favorite songs 等待我最喜爱的歌
4 When they played I'd sing along, 当播放后,我喜欢一个人唱
5 It make me smile 这让我开心的笑了
6 Those were such happy times 像这样快乐的日子
7 and not so long ago 没有多久
8 How I wondered where they'd gone. 我想知道他们去了哪里
9 But they're back again 但他们再次回来
10 just like a long lost friend 像失去很久的朋友
11 All the songs I love so well 所有的歌,我是这么的喜欢
12 Every shalala shalala
13 every wo'wo wo'wo
14 still shines. 依然闪亮
15 Every shing-a-ling-a-ling shing-a-ling-a-ling
that they're starting to sing so fine 他们开始唱歌,是这么美好
When they get to the part 当他们分手
where he's breaking her heart 当他让她伤心
It can really make me cry 这些都让我哭了
just like before. 就像从前一样
It's yesterday once more. 昨日重现
Looking back on 回首看
how it was in years gone by 许多年过去了
And the good times that had 这些好的时光
makes today seem rather sad, 让今天更加难过
So much has changed. 变了这么多
It was songs of love 爱之歌
that I would sing to them 我必须唱给他们听
And I'd memorise each word. 我得记得每个字
Those old melodies 这些古老的旋律
still sound so good to me 我仍然认为非常好听
As they melt the years away 如消失的许多年前一样
Every shalala shalala
every wo'wo wo'wo
still shines. 依然闪亮
Every shing-a-ling-a-ling shing-a-ling-a-ling
that they're starting to sing so fine 他们开始唱歌,是这么美好
All my best memorise come back clearly to me 我的所有美好记忆是这么清晰
Some can even make me cry 一些让我哭泣
just like before. 就像以前一样
It's yesterday once more. 昨日重现


posted @ 2018-01-07 21:49  耐烦不急  阅读(214)  评论(0编辑  收藏  举报