美文网首页
readlines tell

readlines tell

作者: AppleLin_cdea | 来源:发表于2021-10-18 14:19 被阅读0次

    2021年10月18日14:15:22 星期一
    python 文件操作函数 readlines 和 tell 例子

    示例代码 test.py

    import sys
    
    if __name__ == '__main__':
        fn = str(sys.argv[1])
        with open(fn, 'r+') as f:
            print "file name: ", f.name
            print("current postion is "+str(f.tell()))
            line_count = len(f.readlines())
            print("line_count is "+str(line_count))
            print("current postion is "+str(f.tell()))
    

    data.txt

    cat data.txt 
    1 a
    2 b
    3 c
    4 d
    5 e
    6 f
    

    验证

    $ python test.py data.txt 
    file name:  data.txt
    current postion is 0
    line_count is 6
    current postion is 23
    

    相关文章

      网友评论

          本文标题:readlines tell

          本文链接:https://www.haomeiwen.com/subject/qsknoltx.html