标签归档:read-write

以“ rt”和“ wt”模式打开文件

问题:以“ rt”和“ wt”模式打开文件

在这里,我有好几次见过人们使用rtwt模式来读写文件。

例如:

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

我没有看到有关模式的文档,但是由于open()不会引发错误-看起来非常合法。

它的作用是什么,使用wtvs wrtvs 之间有什么区别r

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

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.

What is it for and is there any difference between using wt vs w and rt vs r?


回答 0

t指文本模式。rrtw和与之间没有区别,wt因为文本模式是默认模式。

记录在这里

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')。

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').


回答 1

t显示文本模式,这意味着\n字符将写入文件时,读取时被翻译成主机OS行结束,然后再返回。由于文本模式是默认设置,因此标记基本上只是噪音。

除此之外U,这些模式的标志直接来自标准C库的fopen()功能,即在第六段记录的事实python2文档open()

据我所知,t它不是并且从未成为C标准的一部分,因此尽管C库的许多实现仍然接受了C标准,但并不能保证它们全部都能实现,因此也不能保证它可以在C的每个构建中使用。Python。这就解释了为什么python2文档没有列出它,以及为什么它仍然正常工作。该python3文档使它官员。

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.

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 documentation for 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 docs make it official.


回答 2

“ r”用于阅读,“ w”用于书写,“ a”用于附加。

“ t”表示与二进制模式相对应的文本模式。

因此,我在这里有好几次看到人们使用rt和wt模式读取和写入文件。

编辑:您确定您看到rt而不是rb吗?

这些函数通常包装以下fopen函数:

http://www.cplusplus.com/reference/cstdio/fopen/

如您所见,它提到使用b以二进制模式打开文件。

您提供的文档链接也引用了此b模式:

甚至在没有区别对待二进制文件和文本文件的系统上,将’b’用作文档也是很有用的。

The ‘r’ is for reading, ‘w’ for writing and ‘a’ is for appending.

The ‘t’ represents text mode as apposed to binary mode.

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

Edit: Are you sure you saw rt and not rb?

These functions generally wrap the fopen function which is described here:

http://www.cplusplus.com/reference/cstdio/fopen/

As you can see it mentions the use of b to open the file in binary mode.

The document link you provided also makes reference to this b mode:

Appending ‘b’ is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.


回答 3

t 表示 text mode

https://docs.python.org/release/3.1.5/library/functions.html#open

在Linux上,文本模式和二进制模式之间没有区别,但是在Windows中,它们会转换\n\r\nwhen文本模式。

http://www.cygwin.com/cygwin-ug-net/using-textbinary.html

t indicates for text mode

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.

http://www.cygwin.com/cygwin-ug-net/using-textbinary.html