views:

139

answers:

4

code:

file('pinax/media/a.jpg', 'wb')

thanks

+5  A: 

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.

S.Mark
+2  A: 

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
A: 

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

On Unix systems (Linux, Mac OS X, etc.), binary mode does nothing - they treat text files the same way that any other files are treated. On Windows, however, text files are written with slightly modified line endings. This causes a serious problem when dealing with actual binary files, like exe or jpg files. Therefore, when opening files which are not supposed to be text, even in Unix, you should use wb or rb. Use plain w or r only for text files.

Reference: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files

Daniel G
+1  A: 

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 for more info.

Luiz Damim
This answer would probably be better as a comment to the first answer.
brian buck
Using `file` instead of `open` has been deprecated in Python 2 for some time. It's been *removed* from Python 3.
Scott Griffiths
@Scott: thanks, edited my answer. @brian: you're right, I'll do this next time, thanks.
Luiz Damim