我以二进制格式打开文件(.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()
我尽量避免评估,请尝试以下操作:
file_content_bytes = eval(file_content_str)