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

前端 未结 4 1219
借酒劲吻你
借酒劲吻你 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

提交回复
热议问题