这里有好几次我看到人们使用 rt
和 wt
模式来读写文件。
例如:
with open('input.txt', 'rt') as input_file:
with open('output.txt', 'wt') as output_file:
...
我看不到模式 documented ,但由于 open()
不会引发错误 - 看起来使用起来非常合法。
它的用途是什么?使用 wt
vs w
和 rt
vs r
有什么区别?
最佳答案
t
指的是文本模式。 r
和 rt
或 w
和 wt
之间没有区别,因为文本模式是默认的。
已记录 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)
默认模式为'r'
(打开阅读文字,'rt'
的同义词)。
关于python - 以 'rt' 和 'wt' 模式打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23051062/