In the below section of code, the entire file contents are read at once and
written out. For large files it might be better to read and write a chunk
at a time. Also this method takes a file name and calls open itself, but it
might also be useful to be able pass in a file handle (already opened, just
call write) in case the user doesn't want to write the file to disk. For
example, you could pass in a StringIO object to hold the downloaded file in
memory. (On App Engine, you can't write to the local disk.)
"""Downloads a file from the Document List.
Args:
- uri: string The full Export URL to download the document from.
- file_path: string The full path to save the file to. The export
- format is inferred from the the file extension.
+ uri: string The full Export URL to download the file from.
+ file_path: string The full path to save the file to.
"""
- media_source = self.GetMedia(uri)
+ server_response = self.request('GET', uri)
+ response_body = server_response.read()
+ if server_response.status != 200:
+ raise gdata.service.RequestError, {'status': server_response.status,
+ 'reason': server_response.reason,
+ 'body': response_body}
f = open(file_path, 'wb')
- f.write(media_source.file_handle.read())
+ f.write(response_body)
f.flush()
f.close()
Original issue reported on code.google.com by ericbide...@gmail.com on 9 Apr 2009 at 10:37
Original issue reported on code.google.com by
ericbide...@gmail.com
on 9 Apr 2009 at 10:37