Python 以“rt”和“wt”模式打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23051062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open files in 'rt' and 'wt' modes
提问by alecxe
Several times here on SO I've seen people using rt
and wt
modes for reading and writing files.
好几次在这里,我看到人们使用rt
和wt
模式来读取和写入文件。
For example:
例如:
with open('input.txt', 'rt') as input_file:
with open('output.txt', 'wt') as output_file:
...
I don't see the modes documented, but since open()
doesn't throw an error - looks like it's pretty much legal to use.
我不明白的方式记载,但因为open()
不抛出一个错误-就像这几乎是合法使用的外观。
What is it for and is there any difference between using wt
vs w
and rt
vs r
?
它的用途是什么,使用wt
vsw
和 rt
vs有什么区别r
?
采纳答案by devnull
t
refers to the text mode. There is no difference between r
and rt
or w
and wt
since text mode is the default.
t
指的是文本模式。r
和rt
或w
和之间没有区别,wt
因为文本模式是默认的。
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'
).
默认模式是'r'
(打开阅读文本,同义词'rt'
)。
回答by jussij
The 'r' is for reading, 'w' for writing and 'a' is for appending.
'r' 用于读取,'w' 用于写入,'a' 用于附加。
The 't' represents text mode as apposed to binary mode.
't' 表示与二进制模式相关的文本模式。
Several times here on SO I've seen people using rt and wt modes for reading and writing files.
好几次在这里,我看到人们使用 rt 和 wt 模式来读取和写入文件。
Edit:Are you sure you saw rt and not rb?
编辑:你确定你看到的是 rt 而不是 rb?
These functions generally wrap the fopenfunction which is described here:
这些函数通常包装在这里描述的fopen函数:
http://www.cplusplus.com/reference/cstdio/fopen/
http://www.cplusplus.com/reference/cstdio/fopen/
As you can see it mentions the use of b to open the file in binary mode.
如您所见,它提到使用 b 以二进制模式打开文件。
The document link you provided also makes reference to this b mode:
您提供的文档链接也参考了这种 b 模式:
Appending 'b' is useful even on systems that don't treat binary and text files differently, where it serves as documentation.
附加 'b' 即使在不以不同方式处理二进制文件和文本文件的系统上也很有用,它用作文档。
回答by ymonad
t
indicates for text mode
t
表示为 text mode
https://docs.python.org/release/3.1.5/library/functions.html#open
https://docs.python.org/release/3.1.5/library/functions.html#open
on linux, there's no difference between text mode and binary mode,
however, in windows, they converts \n
to \r\n
when text mode.
在 linux 上,文本模式和二进制模式之间没有区别,但是,在 Windows 中,它们转换\n
为\r\n
文本模式。
回答by ?s??o?
The t
indicates text mode, meaning that \n
characters will be translated to the host OS line endings when writing to a file, and back again when reading. The flag is basically just noise, since text mode is the default.
该t
显示文本模式,这意味着\n
字符将写入文件时,读取时被翻译成主机OS行结束,然后再返回。该标志基本上只是噪音,因为文本模式是默认的。
Other than U
, those mode flags come directly from the standard C library's fopen()
function, a fact that is documented in the sixth paragraph of the python2 documentationfor open()
.
除此之外U
,这些模式的标志直接来自标准C库的fopen()
功能,即在第六段记录的事实python2文档的open()
。
As far as I know, t
is not and has never been part of the C standard, so although many implementations of the C library accept it anyway, there's no guarantee that they all will, and therefore no guarantee that it will work on every build of python. That explains why the python2 docs didn't list it, and why it generally worked anyway. The python3 docsmake it official.
据我所知,t
不是也从来没有成为 C 标准的一部分,所以尽管 C 库的许多实现无论如何都接受它,但不能保证它们都会,因此不能保证它会在每个构建中工作Python。这解释了为什么 python2 文档没有列出它,以及为什么它通常仍然有效。该python3文档使它官员。