我有一个类似于文件的对象StringIO()
,我想将其写入到一个ZipFile()
中,但是我得到了这个TypeError错误:
coercing to Unicode: need string or buffer, cStringIO.StringI found
这里是我使用的代码示例:
file_like = StringIO()
archive = zipfile.ZipFile(file_like, 'w', zipfile.ZIP_DEFLATED)
# my_file is a StringIO object returned by a remote file storage server.
archive.write(my_file)
文档中说StringIO()
是一个类似文件的类,ZipFile()
可以接受类似文件的对象。我是否漏掉了什么?
ZipFile.write()
的参数是一个文件名。 - Sven MarnachZipFile(file_file
而不是ZipFile(file_like
。 - Bogdacutu