See the table on official documentation of the function open
:
That in summary:
r
open the file for reading (keep cursor at the beginning of the file);
w
open the file for writing, delete the current content (keep cursor at the beginning of the file);
+
open the file for update;
It is worth noting that as the mode +
enables file updating when allied to r
also allows writing, while when allied to the w
will also allow reading.
I mean, both ways r+
and w+
you can read and write in the file, the difference is that in the r+
the cursor is moved to the beginning of the file without truncating the content, while in w+
the content is truncated. In other words, write using the mode r+
will overwrite possible contents in the file, while with w+
it will be guaranteed that the file will be blank before writing.
For example, consider a file data.txt
with the initial content Anderson
. When making the change with r+
:
with open('data.txt', 'r+') as stream:
stream.write('Woss')
The final file will be Wossrson
, because the word will override the current content. Already doing with w+
:
with open('data.txt', 'w+') as stream:
stream.write('Woss')
The final file would just be Woss
, since the initial content would be deleted before writing.
Beware of the process of reading and writing on the same file. Doing the reading will naturally move the cursor in the file to the read point and the writing always takes as a basis the position of the cursor. You can make use of the function seek
to manually manage the cursor position without changing the content if necessary.
Allied to this, the way a
will open the write file by moving the cursor to the end of the file:
with open('data.txt', 'a') as stream:
stream.write('Woss')
What would generate the file with AndersonWoss
.
You can see the difference in action in https://editando-arquivo.acwoss.repl.run, the file starts with Conteúdo inicial
, the mode you want to open the file and what you want to write in it is requested. After informing the text the process will resume.
Conteúdo do arquivo: Conteúdo inicial
>>> Qual modo deseja abrir o arquivo? r+
>>> Arquivo aberto com r+, o que deseja escrever? Anderson
Conteúdo do arquivo: Andersono inicial
>>> Qual modo deseja abrir o arquivo? w+
>>> Arquivo aberto com w+, o que deseja escrever? Stack Overflow
Conteúdo do arquivo: Stack Overflow
>>> Qual modo deseja abrir o arquivo? a
>>> Arquivo aberto com a, o que deseja escrever? em Português
Conteúdo do arquivo: Stack Overflow em Português
>>> Qual modo deseja abrir o arquivo?
Realize that even if the initial word Conteúdo
has 8 letters, it will not be completely superimposed when typing Anderson
, which also has 8 letters; this happens due to the accented character ú
occupying 2 bytes, thus the word Conteúdo
occupies 9 bytes while the word Anderson
occupies only 8, which explains the letter o
remain, as it will be in the 9th byte of memory and will not be affected. In short, be very careful with multi-byte encodings.
Source: https://stackoverflow.com/a/30566011/1452488
That summarizing (and translating) stays:
- To read from the beginning:
r
- To write truncating content:
w
- To write from the end (without truncating the content):
a
- To read and write truncating content:
w+
- To read and write from the beginning (without truncating the content):
r+
- To read and write from the end (without truncating the content):
a+
Add the modifier b
if you want to work with binary files.
@Thiagokrempser Yes, it will be written mode, without overwriting the content, which allows reading. However, as you will always be adding to the end of the file, you will need to manually move the cursor before reading (answering the question below - predicted the future xD).
– Woss
There is the way to+?
– Thiago Krempser
I’m sorry I deleted the previous comment, I didn’t mean to.
– Thiago Krempser
@Thiagokrempser No problem :D
– Woss