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

前端 未结 4 1203
借酒劲吻你
借酒劲吻你 2020-12-04 14:27

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         


        
相关标签:
4条回答
  • 2020-12-04 14:57

    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

    0 讨论(0)
  • 2020-12-04 15:03

    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++).

    0 讨论(0)
  • 2020-12-04 15:06

    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.

    0 讨论(0)
  • 2020-12-04 15:09

    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.

    0 讨论(0)
提交回复
热议问题