64

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?

Iamcool
  • 1,387
  • 2
  • 12
  • 24
  • 1
    Hope this [answer](https://stackoverflow.com/a/9644285/9905745) will help, it explains the differences between `'r'` and `'rb'` in python3. – Yossarian42 Oct 04 '19 at 15:21

4 Answers4

102

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.

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • 1
    Please read this: http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files It would answer your question – karthikr Apr 01 '13 at 16:56
  • if I grant write privileges to a file obj where its not required, will that lead to any security issue in future? – Iamcool Apr 01 '13 at 17:05
  • It is risky. Especially in windows as it modifies the file every time we open it – karthikr Apr 01 '13 at 19:03
28

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.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    so, you mean, `r+b` wouldn't make any difference on Linux but does on windows.. moreover, what's the difference between "rb" and "r+b".. – Iamcool Apr 01 '13 at 16:23
  • 3
    I'm not sure you read this answer carefully, @Iamcool. It answers all your questions. – Francis Avila Apr 01 '13 at 17:47
9

My understanding is that adding r+ opens for both read and write (just like w+, though as pointed out in the comment, will truncate the file). The b just opens it in binary mode, which is supposed to be less aware of things like line separators (at least in C++).

Dan Lecocq
  • 3,383
  • 25
  • 22
  • 7
    If I'm not mistaken, "w+" will erase the file if it exists, and then open it for reading and writing. It's not the same as "r+". – Omri Barel Apr 01 '13 at 16:10
4

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. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

Source: Reading and Writing Files

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133