What Does 'Wb' Mean in This Code, Using Python

What does 'wb' mean in this code, using Python?

File mode, write and binary. Since you are writing a .jpg file, it looks fine.

But if you supposed to read that jpg file you need to use 'rb'

More info

On Windows, 'b' appended to the mode
opens the file in binary mode, so
there are also modes like 'rb', 'wb',
and 'r+b'. Python on Windows makes a
distinction between text and binary
files; the end-of-line characters in
text files are automatically altered
slightly when data is read or written.
This behind-the-scenes modification to
file data is fine for ASCII text
files, but it’ll corrupt binary data
like that in JPEG or EXE files.

What do 'rb' and 'wb' mean when opening a file?

From the docs:

As mentioned in the Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is included in the mode argument), the contents of the file are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.

Unsurprisingly, you should use the (default) text mode for text data, and binary mode for binary data – including pickle data, which is explicitly defined as binary:

The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or https://docs.python.org/3/glossary.html#term-bytes-like-object) is converted back into an object hierarchy.

Python 3 approaches the distinction between text and binary data completely differently to Python 2 – indeed, this was the primary reason for the change of major version number. As a result, it's sometimes the case that code which does not adequately consider the distinction appears to "just work" in Python 2 (but will then often bite you in unexpected ways further down the line).

The difference in File access mode w and wb

Absolutely any reference on the fopen() function would have told you this. For instance the manual page which is the common documentation used in Unix-like environments:

The mode string can also include the letter 'b' either as a last character
or as a character between the characters in any of the two-character
strings described above. This is strictly for compatibility with C89 and
has no effect; the 'b' is ignored on all POSIX conforming systems,
including Linux. (Other systems may treat text files and binary files
differently, and adding the 'b' may be a good idea if you do I/O to a
binary file and expect that your program may be ported to non-UNIX
environments.)

So, it stands for binary and is useful to indicate that you intend to treat the contents of the file as not being text.

For your code, binary access seems right. However, directly writing raw struct values is generally a very bad idea, since you don't know the exact internal format used by the compiler and it can change unexpectedly. For files that should be shared and/or accessed "later", this is not the proper way to do it in C. Look into serialization.

Python Pickle Dump 'Wb' parameter

It is not a pickle parameter, but a parameter for the open function. It means: open for writing and open in binary mode.

You can read more in the documentation.

Confused by python file mode w+

Let's say you're opening the file with a with statement like you should be. Then you'd do something like this to read from your file:

with open('somefile.txt', 'w+') as f:
# Note that f has now been truncated to 0 bytes, so you'll only
# be able to read data that you write after this point
f.write('somedata\n')
f.seek(0) # Important: return to the top of the file before reading, otherwise you'll just read an empty string
data = f.read() # Returns 'somedata\n'

Note the f.seek(0) -- if you forget this, the f.read() call will try to read from the end of the file, and will return an empty string.

Difference between modes a, a+, w, w+, and r+ in built-in open function?

The opening modes are exactly the same as those for the C standard library function fopen().

The BSD fopen manpage defines them as follows:

 The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):

``r'' Open text file for reading. The stream is positioned at the
beginning of the file.

``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.

``w'' Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.

``w+'' Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.

``a'' Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.

``a+'' Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.


Related Topics



Leave a reply



Submit