7

I'm using Box Python API to write some tools. Therefore, one of them is to upload a file to Box. They use a StringIO as the object file. I need to read a file locally and write its content to the StringIO buffer, then pass that to the Box API as shown in the code below:

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)

Simple enough, how can I read from a local file, and then write to the StringIO buffer?

1
  • 1
    If you really need a StringIO (likely the file object will do), just do stream.write(open(filename).read()). Commented Dec 14, 2015 at 22:31

1 Answer 1

5

You should be able to supply an open file instead of as StringIO instance. This should do:

stream = open('mylocal_file')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.