文件内需要写入的内容

  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() #关闭连接

执行结果:
  

 

最新文章

  1. sql server 使用nolock提升性能
  2. sublime text install packages报错
  3. 二十、Java基础--------IO流之其他对象
  4. 减去border边框
  5. 三、Android NDK编程预备之Java jni入门创建C/C++共享库
  6. iOS开发中一些常用的方法
  7. java环境变量设置方法
  8. appcan里面模板的使用
  9. VB.NET之错误异常处理
  10. C#深入学习 ----多线程学习(一)第一天学习
  11. 接口测试之webservice介绍
  12. 转: 【Java并发编程】之三:线程挂起、恢复与终止的正确方法(含代码)
  13. mongodb 面试题总结
  14. 【个人笔记】《知了堂》MySQL中的数据类型
  15. golang使用http client发起get和post请求示例
  16. 注解式Schedule配置定时任务
  17. 【Node.js】二、基于Express框架 + 连接MongoDB + 写后端接口
  18. bootstrap栅格系统中同行div高度不一致的解决方法
  19. 数据结构-Python3.7<三>
  20. python第十四课--排序及自定义函数之自定义函数(案例四)

热门文章

  1. [UE4]AWP狙击枪开镜
  2. Unity中进程间通信——使用Protobuf-net序列化与反序列化
  3. hive之权限问题AccessControlException Permission denied: user=root, access=WR
  4. Node JS 8 如何在浏览器上在线调试
  5. window自带的公式面板
  6. Android:真机调试遇到的问题(INSTALL_FAILED_CANCELLED_BY_USER和INSTALL_FAILED_INSUFFICIENT_STORAGE)
  7. 05 Linux系统下的用户以及用户权限管理(权限管理介绍、用户管理、常见命令介绍)
  8. oracle的sqlldr常见问题
  9. Linux 文件,目录,压缩,解压缩操作
  10. python学习之----BuildSoup和正则表达式