如何读取文件,将二进制缓冲区转换为二进制字符串,然后在python中再次将二进制字符串转换为二进制缓冲区?

问题描述 投票:0回答:1

我以二进制格式打开文件(.jpeg),并将其内容存储在变量中,然后使用str()将二进制缓冲区转换为字符串。现在,我想再次将字符串转换为二进制缓冲区。

from tkinter import filedialog
file_path = filedialog.askopenfilename()
file = open(file_path,"rb")
file_content = file.read()
file.close()
print(file_content)
file_content_str = str(file_content)
print(file_content)

# want a code to convert file_content_str into bytes again here
# file_content_bytes = file_content_string converted to bytes

# file2 = open("moon2.jpg", "w+b")
# file2.write(file_content_bytes)
# file2.close()

python file-handling
1个回答
0
投票

我尽量避免评估,请尝试以下操作:

file_content_bytes = eval(file_content_str)

© www.soinside.com 2019 - 2024. All rights reserved.