Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between rb and r+b modes in file objects

I am using pickle module in Python and trying different file IO modes:

# works on windows.. "rb" with open(pickle_f, 'rb') as fhand:         obj = pickle.load(fhand)  # works on linux.. "r" with open(pickle_f, 'r') as fhand:         obj = pickle.load(fhand)  # works on both "r+b" with open(pickle_f, 'r+b') as fhand:         obj = pickle.load(fhand) 

I never read about "r+b" mode anywhere, but found mentioning about it in the documentation.

I am getting EOFError on Linux if I use "rb" mode and on Windows if "r" is used. I just gave "r+b" mode a shot and it's working on both.

What's "r+b" mode? What's the difference between "rb" and "r+b"? Why does it work when the others don't?

like image 439
Iamcool Avatar asked Apr 01 '13 16:04

Iamcool


2 Answers

r+ is used for reading, and writing mode. b is for binary. r+b mode is open the binary file in read or write mode.
You can read more here.

like image 178
karthikr Avatar answered Sep 28 '22 20:09

karthikr


r opens for reading, whereas r+ opens for reading and writing. The b is for binary.

This is spelled out in the documentation:

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.

like image 39
NPE Avatar answered Sep 28 '22 20:09

NPE