Why can't Python on Windows read an image in binary mode?

I want to read an image in binary mode so that I can save it to my database, for example:

img = open("Last_Dawn.jpg")
t = img.read()
save_to_db(t)

This works on a Mac. But on Windows, that img.read () is wrong. This is a bit of the whole set.

So my first question is: why does the code above not work on Windows?

And second: is there any other way to do this?

Thank you so much!

+1
source share
4 answers

I can’t say for sure, but I know that the ISO C standard does not distinguish between binary and non-binary modes when called, fopenand yet Windows does.

, Python fopen("Last_Dawn.jpg","r") ( C), Windows .

(LF -> CRLF) , , .

"rb" , :

img = open("Last_Dawn.jpg", "rb")
+2

:

img = open("Last_Dawn.jpg", 'rb')
+6

Python, :

img = open('whatever.whatever', 'rb')

. open : http://docs.python.org/library/functions.html#open

+4
open(filename, 'rb')
+2

Source: https://habr.com/ru/post/1774764/


All Articles