Python之把字节串写入文件然后从文件中读出字节串

Python之把字节串写入文件然后从文件中读出字节串


以下是将字节串写入文件并从文件中读出字节串的Python代码示例:

# 将字节串写入文件
byte_string = b'Hello, world!'
with open('file.bin', 'wb') as f:
    f.write(byte_string)

# 从文件中读出字节串
with open('file.bin', 'rb') as f:
    byte_string = f.read()

print(byte_string)  # 输出:b'Hello, world!'

在上述示例中,我们使用open()函数打开一个二进制文件(即'wb''rb'模式),然后使用write()read()方法将字节串写入文件并从文件中读出字节串。注意,在写入文件时,我们需要将字节串对象作为参数传递给write()方法,而在读取文件时,read()方法将返回一个字节串对象。

《AUTOSAR谱系分解(ETAS工具链)》之总目录

Guess you like

Origin blog.csdn.net/PlutoZuo/article/details/134684230