Open files in 'rt' and 'wt' modes
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)
The default mode is
'r' (open for reading text, synonym of
★ Back to homepage or read more recommendations: