Several times here on SO I’ve seen people using
wt modes for reading and writing files.
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
t refers to the text mode. There is no difference between
wt since text mode is the default.
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)
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.
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.
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:
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.
t indicates for
on linux, there’s no difference between text mode and binary mode,
however, in windows, they converts
\r\n when text mode.