python - 以 'rt' 和 'wt' 模式打开文件

标签 python file file-io read-write

这里有好几次我看到人们使用 rtwt 模式来读写文件。

例如:

with open('input.txt', 'rt') as input_file:
     with open('output.txt', 'wt') as output_file: 
         ...

我看不到模式 documented ,但由于 open() 不会引发错误 - 看起来使用起来非常合法。

它的用途是什么?使用 wt vs wrt vs r 有什么区别?

最佳答案

t 指的是文本模式。 rrtwwt 之间没有区别,因为文本模式是默认的。

已记录 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/

相关文章:

c++ - 错误:无法转换为简单的 std::string 文件 slurper

python - Pandas 时间序列事件之间的时间

file - 实现Semi-Round-Robin文件,可按需扩展和保存

python - python中的文件路径

java:从文件开头查找文本文件中单词的字符偏移量,对数偏移量?

c# - 只允许一台服务器访问网络驱动器上的文件

python - 如果常量是特定值,则省略多个 where 条件之一

python - Tkinter回调异常: matplotlib animation

Python:如果不是 val,vs 如果 val 是 None

python - 创建和初始化 python 列表