143

Code:

file('pinax/media/a.jpg', 'wb')
Random Nerd
  • 136
  • 1
  • 9
zjm1126
  • 58,281
  • 75
  • 169
  • 215
  • 1
    Use `open` instead of `file`, which was deprecated in Python 2 and removed in Python 3. See https://stackoverflow.com/questions/112970/python-when-to-use-file-vs-open – Max Ghenis Jan 08 '19 at 20:10
  • Also you should consider using `open` instead of `file`. `file` was deprecated in Python 2 (couldn't find which version) and has been removed in py3k. (thanks Scott) See [this question](https://stackoverflow.com/questions/112970/python-when-to-use-file-vs-open) for more info. – Luiz Damim Apr 19 '10 at 13:16
  • 2
    It is not really fair to assume it is the OP's own code, especially given the nature of the question. It was obviously valid at one time. – mckenzm May 02 '19 at 03:35

4 Answers4

146

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.

YOU
  • 114,140
  • 31
  • 183
  • 213
  • 4
    Concretely, in Windows for a file opened in text mode, `fd.write("foo\n")` actually writes on disk `foo\r\n` (note the `\r`). – Serge Ballesta Aug 21 '14 at 05:48
  • 4
    I am pretty sure 'b' opens files in binary mode on every platform, not just Windows or there would be an enormous caveat in here as well. – mckenzm May 02 '19 at 03:38
110

The wb indicates that the file is opened for writing in binary mode.

When writing in binary mode, Python makes no changes to data as it is written to the file. In text mode (when the b is excluded as in just w or when you specify text mode with wt), however, Python will encode the text based on the default text encoding. Additionally, Python will convert line endings (\n) to whatever the platform-specific line ending is, which would corrupt a binary file like an exe or png file.

Text mode should therefore be used when writing text files (whether using plain text or a text-based format like CSV), while binary mode must be used when writing non-text files like images.

References:

https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files https://docs.python.org/3/library/functions.html#open

Daniel G
  • 62,706
  • 7
  • 41
  • 41
  • 1
    This may have changed over time. On Ubuntu 18.04 running Python 3.6.8, the 'binary mode' definitely mattered. I got an error trying to write to a text file (CSV format, not that it ultimately matters that much) that was opened with the `wb` option. By using the `w` option instead, I was able to get it to work properly. – TheDavidJohnson Oct 01 '19 at 16:12
  • 2
    Thanks for your comment @TheDavidJohnson. Back in Python 2.6, the docs said that the `b` mode only had an effect on Windows. That has now been removed from the documentation and binary mode "should be used for all files that don’t contain text". – Daniel G Oct 01 '19 at 18:21
  • 2
    Sure thing, @Daniel! I appreciate you posting your solution. More than 9 years later, it's still helpful. How great is that? In any case, I just wanted to add some new info for others like me who might still come along and find this helpful. Cheers! – TheDavidJohnson Oct 02 '19 at 21:16
  • 2
    Great explanation. One clarification, however: you say _"Text mode should therefore be used when writing text files"_. For completeness I'd refer back to your earlier comment that "Python makes no changes [in binary mode]", and add that you would use binary mode on a text file when either don't know (or care) what the encoding is but just need to read or write the bytes, or if you want to preserve the line endings regardless of platform. – pcdev Mar 17 '20 at 21:01
8

That is the mode with which you are opening the file. "wb" means that you are writing to the file (w), and that you are writing in binary mode (b).

Check out the documentation for more: clicky

GlenCrawford
  • 3,269
  • 3
  • 24
  • 33
-2

Yeah, many peoples getting confuse to understand what is "b"; Actually in computer programming having various data type; "b" is 'byte' data type and it's 8 bits long; When you open an image file you can see "{ 0xFF, 0xF0, 0x0F, 0x11 }" this kinds of text and it's byte data; yes that's right "b" means binary data but another mean of "b" is 'byte' data in Python "wb" means "write+byte"..

Techno TM
  • 1
  • 1