Open files in 'rt' and 'wt' modes

后端 未结 4 1562
故里飘歌
故里飘歌 2020-11-29 19:54

Several times here on SO I\'ve seen people using rt and wt modes for reading and writing files.

For example:

with open(\'in         


        
4条回答
  •  日久生厌
    2020-11-29 20:42

    t refers to the text mode. There is no difference between r and rt or w and wt since text mode is the default.

    Documented here:

    Character   Meaning
    'r'     open for reading (default)
    'w'     open for writing, truncating the file first
    'x'     open for exclusive creation, failing if the file already exists
    'a'     open for writing, appending to the end of the file if it exists
    'b'     binary mode
    't'     text mode (default)
    '+'     open a disk file for updating (reading and writing)
    'U'     universal newlines mode (deprecated)
    

    The default mode is 'r' (open for reading text, synonym of 'rt').

提交回复
热议问题