172

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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195

4 Answers4

282

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

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
devnull
  • 118,548
  • 33
  • 236
  • 227
  • 7
    Gotcha, it's documented in python3 docs. So, there is basically no difference between `wt` vs `w` and `rt` vs `r` - just `explicit is better than implicit`? – alecxe Apr 14 '14 at 02:38
  • @alecxe Right, since text mode is the default, there is no difference between `r` and `rt`... – devnull Apr 14 '14 at 02:39
  • 23
    Note that `w` isn't always equal to `wt`. One such case is [`gzip.open`](https://docs.python.org/3/library/gzip.html#gzip.open) where binary mode is default, and not text mode. Related question: http://stackoverflow.com/questions/42013083/python-3-gzip-open-and-modes – Carl Ekerot Mar 08 '17 at 14:45
15

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.

ʇsәɹoɈ
  • 22,757
  • 7
  • 55
  • 61
4

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.

jussij
  • 10,370
  • 1
  • 33
  • 49
  • Yeah, it was clearly `rt`, e.g. http://stackoverflow.com/questions/10971033/backporting-python-3-openencoding-utf-8-to-python-2, or http://stackoverflow.com/questions/17127853/are-openfile-wt-or-rt-different-objects etc. Thank you for the info, good to know. – alecxe Apr 14 '14 at 02:46
  • In the link that devnull provides the 't' text option is listed. What surprised me was the C++ link did not also mention that 't' option as I'm pretty sure I'd used the 'rt' and 'wt' options in C fopen code written years ago. – jussij Apr 14 '14 at 03:08
  • Yeah, that's why I've asked - it was like a non-documented feature for me. Hope the thread would help someone in the future. Thanks again. – alecxe Apr 14 '14 at 03:14
3

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

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
ymonad
  • 11,710
  • 1
  • 38
  • 49
  • 5
    In Python 3 there's an additional difference between text and binary file modes (on all platforms). In text mode, `read` returns Unicode strings. In binary mode, `read` returns a `bytes` instance. If you want to write Python 2 code with forwards compatibility in mind, you can use `io.open` rather than the standard `open` to get the Python 3 behavior (with `unicode` versus `str` instances). – Blckknght May 01 '14 at 02:38