Wondering what the real difference is when writing files from Python. From what I can see if I use w
or wb
I am getting the same result with text.
I thought that saving as a binary file would show only binary values in a hex editor, but it also shows text and then ASCII version of that text.
Can both be used interchangably when saving text? (Windows User)
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.
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.
Explanation: wb mode is used to open binary file in write mode and wb+ mode open binary file both for read and write operation.
w+: Opens a file in read and write mode. It creates a new file if it does not exist, if it exists, it erases the contents of the file and the file pointer starts from the beginning. rw+: Opens a file in read and write mode. File pointer starts at the beginning of the file.
Only in Windows, in the latter case, .write('\n')
writes one byte with a value of 10. In the former case, it writes two bytes, with the values 13 and 10.
You can prove this to yourself by looking at the resulting file size, and examining the files in a hex editor.
In POSIX-related operating systems (UNIX, SunOS, MacOS, Linux, etc.), there is no difference beetween 'w'
and 'wb'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With