1 回答
TA贡献1853条经验 获得超18个赞
或者,如果 API 需要类似文件的对象,您可以在创建 zip 文件时传递BytesIO实例并将其传递给 API
import io
def create_extension_zip_file(self, path_to_extension_directory, directory_name):
buf = io.BytesIO()
zipObj = ZipFile(buf, "w")
with zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(path_to_extension_directory):
for filename in filenames:
# create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
with open(filename, 'rb') as file_data:
bytes_content = file_data.read()
# Add file to zip
zipObj.write(bytes_content, basename(filePath))
# Rewind the buffer's file pointer (may not be necessary)
buf.seek(0)
return buf
如果 API 需要 bytes 实例,您可以在写入后以二进制模式打开 zip 文件,然后传递 bytes 。
with open('static_extension.zip', 'rb') as f:
bytes_ = f.read()
添加回答
举报