我正在使用Box
pythonapi编写一些工具。因此,其中之一是将文件上载到Box
。它们使用StringIO
作为对象文件。
我需要在本地读取一个文件并将其内容写入StringIO
缓冲区,然后将其传递给Box
API,如下面的代码所示:
def upload_file(self, filename, folder_id='0'):
assert self.client is not None
try:
stream = StringIO.StringIO()
# replace this line a file read
stream.write('Box Python SDK Test!')
stream.seek(0)
box_file = self.client.folder(folder_id=folder_id).upload_stream(
stream, filename,
preflight_check=True)
return box_file.name
except BoxAPIException, e:
self.log.exception(e)
很简单,如何从本地文件读取,然后写入StringIO
缓冲区?在
您应该能够提供一个打开的文件,而不是
StringIO
实例。这应该做到:相关问题 更多 >
编程相关推荐