Python IO file 'w' vs 'wb'

It's amazing what the real difference is when writing files with Python. From what I see, if I use w or wb , I get the same result with text.

I thought that saving as a binary would only show binary values ​​in a hex editor, but also display the text, and then the ASCII version of that text.

Can both users be used interchangeably when saving text? (Windows user)

+4
source share
1 answer

Only on Windows, in the latter case, .write('\n') writes one byte with a value of 10. In the first case, it writes two bytes with a value of 13 and 10.

You can prove it yourself by looking at the resulting file size and view the files in a hex editor.

On POSIX-related operating systems (UNIX, SunOS, MacOS, Linux, etc.), there is no difference between 'w' and 'wb' .

+5
source

All Articles