python open mode

  • 必须其一: r, w, a: read(默认), write, append

  • 可选: b, t, +, U: binary, text(默认), reading and writing, universal newlines mode

  • rt == r, wt == w, at == a

  • r+

    >>> with open('aa', 'r+') as f:
    ...     print(f.tell())
    ...
    0
    >>> with open('aa', 'r+') as f:
    ...     f.readline()
    ...     print(f.tell())
    ...
    '0\n'
    2
    
    • read执行在前, write执行在后: 添加到最后一行
    • write执行在前: 写入并替换第一行内容
  • w+ == w

  • a+ == a

posted @ 2020-04-10 16:54  twfb  阅读(404)  评论(0编辑  收藏  举报