• Python3 文件读写注意事项(指针问题)


     1 C:UsersAdministratorAppDataLocalProgramsPythonPython35python.exe E:/python/day2/op.py
     2 Somehow, it seems the love I knew was always the most destructive kind
     3 不知为何,我经历的爱情总是最具毁灭性的的那种
     4 Yesterday when I was young
     5 昨日当我年少轻狂
     6 The taste of life was sweet
     7 生命的滋味是甜的
     8 As rain upon my tongue
     9 就如舌尖上的雨露
    10 I teased at life as if it were a foolish game
    11 我戏弄生命 视其为愚蠢的游戏
    12 The way the evening breeze
    13 就如夜晚的微风
    14 May tease the candle flame
    15 逗弄蜡烛的火苗
    16 The thousand dreams I dreamed
    17 我曾千万次梦见
    18 The splendid things I planned
    19 那些我计划的绚丽蓝图
    20 I always built to last on weak and shifting sand
    21 但我总是将之建筑在易逝的流沙上
    22 I lived by night and shunned the naked light of day
    23 我夜夜笙歌 逃避白昼赤裸的阳光
    24 And only now I see how the time ran away
    25 事到如今我才看清岁月是如何匆匆流逝
    26 Yesterday when I was young
    27 昨日当我年少轻狂
    28 So many lovely songs were waiting to be sung
    29 有那么多甜美的曲儿等我歌唱
    30 So many wild pleasures lay in store for me
    31 有那么多肆意的快乐等我享受
    32 And so much pain my eyes refused to see
    33 还有那么多痛苦 我的双眼却视而不见
    34 I ran so fast that time and youth at last ran out
    35 我飞快地奔走 最终时光与青春消逝殆尽
    36 I never stopped to think what life was all about
    37 我从未停下脚步去思考生命的意义
    38 And every conversation that I can now recall
    39 如今回想起的所有对话
    40 Concerned itself with me and nothing else at all
    41 除了和我相关的 什么都记不得了
    42 The game of love I played with arrogance and pride
    43 我用自负和傲慢玩着爱情的游戏
    44 And every flame I lit too quickly, quickly died
    45 所有我点燃的火焰都熄灭得太快
    46 The friends I made all somehow seemed to slip away
    47 所有我交的朋友似乎都不知不觉地离开了
    48 And only now I'm left alone to end the play, yeah
    49 只剩我一个人在台上来结束这场闹剧
    50 Oh, yesterday when I was young
    51 噢 昨日当我年少轻狂
    52 So many, many songs were waiting to be sung
    53 有那么那么多甜美的曲儿等我歌唱
    54 So many wild pleasures lay in store for me
    55 有那么多肆意的快乐等我享受
    56 And so much pain my eyes refused to see
    57 还有那么多痛苦 我的双眼却视而不见
    58 There are so many songs in me that won't be sung
    59 我有太多歌曲永远不会被唱起
    60 I feel the bitter taste of tears upon my tongue
    61 我尝到了舌尖泪水的苦涩滋味
    62 The time has come for me to pay for yesterday
    63 终于到了付出代价的时间 为了昨日
    64 When I was young
    65 当我年少轻狂 111111111111111!!!!!!! 
    View Code
    1 f = open('yesterday') #文件句柄
    2 data = f.read()
    3 data2 = f.read()
    4 print(data,'111111111111111!!!!!!!',data2)
    View Code

     这里我们打印了print(data1,data2),但是为什么data2没有read出来?

    很简单,python文件读取中遵循着一个指针,可以理解为我们打字光标,当你打字打了很多的时候,光标不停往后移动,理解为read不停读取字符串,但是当读取完的时候,光标不会自动移回去,所以我们要手动的移回去才能在读取一遍,不然光标后面就无字符串可读取,所以没有print出来。

    1 f.seek(0)#调整指针
    View Code
  • 相关阅读:
    javax.xml.ws.WebServiceException: Provider com.sun.xml.ws.spi.ProviderImpl not found
    注意资源利用 不然导致资源消耗会很严重
    E212: 不能以写入模式打开 linux
    安装db2 提示不是有效的win32应用程序?
    对苹果“五仁”编程语言Swift的简单分析
    Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字
    Oracle 学习笔记 14 -- 集合操作和高级子查询
    PHP的curl库代码使用
    AWS OpsWorks新增Amazon RDS支持
    最短编辑距离算法
  • 原文地址:https://www.cnblogs.com/Tsukasa/p/6550284.html
Copyright © 2020-2023  润新知