流觞区直

导航

4.Python文件操作

 

文件内需要写入的内容

  Seems the love I’ve ever known 
  看来,过去我所知道的爱情 
  Has always been the most destructive kind 
  似乎总是最具有毁灭性的那种 
  Guess that’s why now 
  或许,那就是为什么 
  I feel so old before my time 
  如今我感觉如此的未老先衰 
  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 a candle flame 
  就好像夜晚的微风逗弄著一盏烛火那样 
  
  The thousand dreams I dreamed, the splendid things I planned, 
  成千个我所做过的梦,还有那些我所计画的大业 
  I always built, alas, on weak and shifting sand, 
  我总是建筑在,唉,松软的流沙上 
  I lived by night and shunned the naked light of day, 
  我夜夜笙歌,躲避著白昼赤裸的阳光 
  And only now I see how the years ran away 
  直到现在,我才惊觉岁月已经如何的消逝 
  
  Yesterday, when I was young, 
  昨日,当我轻狂年少 
  So many happy songs were waiting to be sung, 
  那么多快乐的歌曲等待我去唱 
  So many wild pleasures lay in store for me, 
  那么多狂野的乐趣等待我去享用 
  And so much pain my dazzled 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 I can now recall, 
  而如今我能够记得的所有对话 
  Concerned itself with me, and nothing else at all 
  都只跟我有关,其他的什么也没有 
  
  Yesterday, the moon was blue, 
  昨日,当月光依旧湛蓝 
  And every crazy day brought something new to do, 
  而每个疯狂的日子都带来一些新鲜的事情可作 
  I used my magic age as if it were a wand, 
  我滥用著我神奇的年纪,就像它是根魔法棒 
  And never saw the waste and emptiness beyond 
  从来没有看见背后的浪费与空虚 
  
  The game of love I played with arrogance and pride, 
  我用轻狂与自负的态度,玩著爱情的游戏 
  And every flame I lit too quickly, quickly died, 
  而我所点燃的所有焰火,都太快太快的熄灭 
  The friends I made all seemed somehow to drift away, 
  我所交的朋友,似乎都一一逐渐远去 
  And only I am left on stage to end the play 
  只有我被留在舞台上,独自去结束这场戏 
  
  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 ,之后对打开后的文件的所有操作都通过f 来进行:

f = open("yesterday",encoding="utf-8")  # 注意:须标明字符集 #这一段是文件句柄

  

f = open("yesterday",encoding="utf-8")
data = f.read()
data2 = f.read()

print(data)
print("-------------------")
print(data2)

执行结果:
  

为什么data2没有结果?
 答:是因为第一次读取了文件后,文件的指针指向了文件末尾,导致第二次读取文件的时候是从末尾开始读的,末尾之后是没有内容的,所以读取到的内容为空,此时,只需在第一次读取后重新再获取下文件的位置即可:

#!/usr/bin/python3

f = open("yesterday",encoding="utf-8")
data = f.read()
f = open("yesterday",encoding="utf-8") #指针重新指向文件开始位置
data2 = f.read()

print(data)
print("-------------------")
print(data2)

执行结果:
 

另外一种方法f.seek()方式 f.seek(0)即可指会到文件最开始的位置:

#!/usr/bin/python3

f = open("yesterday","r",encoding="utf-8")

print(f.readline())
print(f.readline())
print(f.readline())
print("当前所在位置:")
print(f.tell()) # tell()记录光标所在位置

f.seek(0) #将光标指到最开始的位置
print("当前所在位置:")
print(f.tell())

print(f.readline())
print("当前所在位置:")
print(f.tell())
f.close()

  

执行结果:
 

 

对文件内容进行追加(a:attend)写操作:
#ps: a 只能进行写操作,不能进行读取操作,读取只能使用 r ,创建一个新文件并写入数据是 w.

#!/usr/bin/python3

f = open("yesterday3",'w',encoding="utf-8") 
f.write("我爱北京天安门,\n")
f.write("天安门上太阳升。\n")
f = open("yesterday3",'a',encoding="utf-8") 
f.write("追加写入-------->\n") f.write("我爱北京天安门,\n") f.write("天安门上太阳升。\n")
f = open("yesterday3",'r',encoding="utf-8") #以只读方式打开
data = f.read()
print(data)
f.close() #关闭连接

执行结果:
  

 

 

posted on 2019-03-24 12:47  流觞区直  阅读(231)  评论(0编辑  收藏  举报