700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python读写二进制文件(读写字节数据)

python读写二进制文件(读写字节数据)

时间:2024-02-01 15:10:41

相关推荐

python读写二进制文件(读写字节数据)

python读写二进制文件(读写字节数据)

你想读写二进制文件,比如图片,声音文件等就是常见的二进制文件。

使用模式为rbwbopen()函数来读取或写入二进制数据。比如:

# Read the entire file as a single byte stringwith open(somefile.bin, b) as f:data = f.read()# Write binary data to a filewith open(somefile.bin, wb) as f:f.write(bHello World)

在读取二进制数据时,需要指明的是所有返回的数据都是字节字符串格式的,而不是文本字符串。 类似的,在写入的时候,必须保证参数是以字节形式对外暴露数据的对象(比如字节字符串,字节数组对象等)。

# 分别进行数据的读取和写入

Heres how to do it with the basic file operations in Python. This opens one file, reads the data into memory, then opens the second file and writes it out.

in_file = open("in-file", "rb"

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。