文件对象中的 rb 和 r+b 模式有什么区别 [英] What is the difference between rb and r+b modes in file objects

查看:18
本文介绍了文件对象中的 rb 和 r+b 模式有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中使用 pickle 模块并尝试不同的文件 IO 模式:

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)

我从未在任何地方读到过r+b"模式,但在 文档.

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

如果我在 Linux 上使用 "rb" 模式和在 Windows 上如果使用 "r",我会收到 EOFError.我刚刚给了 "r+b" 模式一个镜头,它对这两种模式都有效.

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.

什么是"r+b" 模式?"rb" 和 "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 用于二进制.r+b 模式是以读或写模式打开二进制文件.
您可以在此处阅读更多内容.

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.

这篇关于文件对象中的 rb 和 r+b 模式有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆