首页 > 解决方案 > 如何将目录中的多个文档转换为base64字符串并保存在文本文件中

问题描述

我想将特定目录中的多个简历转换为base64字符串并同时保存到文本文件中。

到目前为止我尝试了什么

import base64
import sys

with open("filename.pdf", "rb") as pdf_file , open("filename.pdf","w") as output:
    encoded_string = base64.b64encode(pdf_file.read(),output.write())

I got this error when I execute the code

Traceback (most recent call last):
  File "encode.py", line 5, in <module>
    encoded_string = base64.b64encode(pdf_file.read(),output.write())
TypeError: write() takes exactly one argument (0 given)

标签: pythonpython-3.x

解决方案


应该:

output.write(base64.b64encode(pdf_file.read()))

或者:

encoded_string = base64.b64encode(pdf_file.read())
output.write(encoded_string)

推荐阅读