Python 文件对象中的 rb 和 r+b 模式有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15746954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between rb and r+b modes in file objects
提问by Iamcool
I am using pickle module in Python and trying different file IO modes:
我在 Python 中使用 pickle 模块并尝试不同的文件 IO 模式:
# 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.
我从来没有在任何地方读过“r+b”模式,但在文档中发现提到它。
I am getting EOFErroron 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.
我得到EOFError在Linux上,如果我使用的"rb"模式,在windows下"r"使用。我只是给了"r+b"模式一个镜头,它对两者都有效。
What's "r+b"mode? What's the difference between "rb" and "r+b"? Why does it work when the others don't?
什么是"r+b"模式?"rb" 和 "r+b" 和有什么不一样?为什么它会起作用而其他人不起作用?
采纳答案by karthikr
回答by Dan Lecocq
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 bjust opens it in binary mode, which is supposed to be less aware of things like line separators (at least in C++).
我的理解是,r+为读取和写入添加打开(就像w+,尽管在评论中指出,将截断文件)。在b刚刚打开它以二进制模式,这被认为是少知道像的行分隔的东西(至少在C ++)。
回答by NPE
ropens for reading, whereas r+opens for reading and writing. The bis for binary.
r为阅读而r+打开,而为阅读和写作而打开。的b是二进制。
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.
mode 最常用的值是
'r'读取、'w'写入(如果文件已经存在则截断文件)和'a'追加(在某些 Unix 系统上意味着所有写入都附加到文件末尾,而不管当前的查找位置如何)。如果省略 mode,则默认为'r'。默认是使用文本模式,它可以'\n'在写入和读取时将字符转换为特定于平台的表示。因此,在打开二进制文件时,您应该附加'b'模式值以二进制模式打开文件,这将提高可移植性。('b'即使在不以不同方式对待二进制文件和文本文件的系统上,追加也很有用,它用作文档。)有关更多可能的模式值,请参见下文。Modes
'r+','w+'并'a+'打开文件进行更新(注意会'w+'截断文件)。'b'在区分二进制和文本文件的系统上,附加到模式以二进制模式打开文件;在没有这种区别的系统上,添加'b'无效。
回答by Chankey Pathak
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.
在 Windows 上,附加到模式的 'b' 以二进制模式打开文件,因此还有像 'rb'、'wb' 和 'r+b' 这样的模式。Windows 上的 Python 区分文本文件和二进制文件;读取或写入数据时,文本文件中的行尾字符会自动稍微改变。这种对文件数据的幕后修改适用于 ASCII 文本文件,但它会破坏 JPEG 或 EXE 文件中的二进制数据。读写此类文件时要非常小心地使用二进制模式。在 Unix 上,将 'b' 附加到模式并没有什么坏处,因此您可以独立于平台对所有二进制文件使用它。
Source: Reading and Writing Files
来源:读写文件

