Uso l'API Python Box
per scrivere alcuni strumenti. Pertanto, uno di questi è caricare un file su Box
. Usano un StringIO
come file oggetto. ho bisogno di leggere un file localmente e scrivere il suo contenuto al buffer StringIO
, quindi passare che al Box
API come mostrato nel seguente codice:Lettura da un file e scrittura in StringIO - Python
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)
abbastanza semplice, come posso leggere da un file locale, e quindi scrivere sul buffer StringIO
?
Se hai davvero bisogno di un StringIO (probabilmente l'oggetto file lo farà), fai semplicemente 'stream.write (open (filename) .read())'. – tdelaney