从文件读取并写入StringIO Python

2024-10-01 22:33:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Boxpythonapi编写一些工具。因此,其中之一是将文件上载到Box。它们使用StringIO作为对象文件。 我需要在本地读取一个文件并将其内容写入StringIO缓冲区,然后将其传递给BoxAPI,如下面的代码所示:

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缓冲区?在


Tags: 文件工具selfboxclientidstreamfolder

热门问题