157

Code:

file('pinax/media/a.jpg', 'wb')
Random Nerd
  • 134
  • 1
  • 9
zjm1126
  • 63,397
  • 81
  • 173
  • 221
  • 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
  • 1
    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

5 Answers5

159

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
  • 120,166
  • 34
  • 186
  • 219
  • 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
117

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
  • 67,224
  • 7
  • 42
  • 42
  • 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
9

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,359
  • 3
  • 26
  • 34
-2

I think it is write only in binary mode, also you can check others mode:

  • r: Opens the file in read-only mode. Starts reading from the beginning of the file and is the default mode for the open() function.
  • rb: Opens the file as read-only in binary format and starts reading from the beginning of the file. While binary format can be used for different purposes, it is usually used when dealing with things like images, videos, etc.
  • r+: Opens a file for reading and writing, placing the pointer at the beginning of the file.
  • w: Opens in write-only mode. The pointer is placed at the beginning of the file and this will overwrite any existing file with the same name. It will create a new file if one with the same name doesn't exist.
  • wb: Opens a write-only file in binary mode.
  • w+: Opens a file for writing and reading.
  • wb+: Opens a file for writing and reading in binary mode.
  • a: Opens a file for appending new information to it. The pointer is placed at the end of the file. A new file is created if one with the same name doesn't exist.
  • ab: Opens a file for appending in binary mode.
  • a+: Opens a file for both appending and reading.
  • ab+: Opens a file for both appending and reading in binary mode.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Welcome to Stack Overflow! Other than the first line, your answer appears likely to have been written (entirely or partially) by AI (e.g., ChatGPT) or copied from some other source. Please be aware that [posting of AI-generated content is banned here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. If you copied it from somewhere, [you must cite your source](https://meta.stackexchange.com/q/160077/902710). Thanks! – NotTheDr01ds Jun 12 '23 at 20:30
  • It's also not clear why you would add essentially the same answer to the question that was already written 13 years ago, accepted, and upvoted. It would be more helpful for you to find questions that need answering, or if there's outdated information in an older question, then post more recent information. Thanks! – NotTheDr01ds Jun 12 '23 at 20:33
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 13 '23 at 10:38
  • This is regular plagiarism. It was likely copied wholesale from *[File Handling in Python](https://stackabuse.com/file-handling-in-python/)* (though the real source may be somewhere else; I don't know how reputable *StackAbuse* is), near *"The access modes available for the open() function are as follows"*. Though it was poorly formatted (the list formatting was lost). – Peter Mortensen Jun 13 '23 at 11:44
  • @NotTheDr01ds: Regular plagiarism hasn't gone out of fashion yet. I also saw several examples yesterday (though ChatGPT plagiarism is dominating) – Peter Mortensen Jun 13 '23 at 11:50
  • @PeterMortensen Right, I had a feeling this was "some other source", but I just went with both possibilities to be sure ;-). Probably should have dropped the "big bold" warning for other users here, though. – NotTheDr01ds Jun 13 '23 at 14:21
-6

Yeah, many people are getting confused to understand what "b" is.

Actually, in computer programming having various data types.

"b" is 'byte' data type and it's 8 bits long. When you open an image file, you can see "{ 0xFF, 0xF0, 0x0F, 0x11 }". These kinds of text and it's byte data.

Yes, that's right. "b" means binary data, but another meaning of "b" is 'byte' data in Python and "wb" means "write+byte"...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Techno TM
  • 1
  • 1