https://docs.python.org/3/library/gzip.html
I am considering to use gzip.open()
, and I am a little confused about the mode
argument:
The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x' or 'xb' for binary mode, or 'rt', 'at', 'wt', or 'xt' for text mode. The default is 'rb'.
So what is the difference between 'w'
and 'wb'
?
The document states they are both binary mode.
So does that mean that there is no difference between 'w'
and 'wb'
?
Exactly as you say and as already covered by @
Jean-François Fabre answer.
I just wanted to show some code, as it was fun.
Let's have a look at the
gzip.py
source code in the python library to see that's effectively what happens.The
gzip.open()
can be found here https://github.com/python/cpython/blob/master/Lib/gzip.py and I report belowFew things we notice:
rb
as the documentation you report saysto open a binary file, it doesn't care whether it's
"r", "rb", "w", "wb"
for example.This we can see in the following lines:
basically the binary file
binary_file
gets built wether there's an additional b or not asgz_mode
can have theb
or not at this point.Now the class
class GzipFile(_compression.BaseStream)
is called to buildbinary_file
.In the constructor the following lines are important:
where can be clearly seen that if
'b'
is not present in the mode it will be addedso there's no distinction between the two modes as already discussed.
It means that
r
defaults torb
, and if you want text you have to specify it usingrt
.(as opposed to
open
behaviour wherer
meansrt
, notrb
)