From 89a816a4fc30b562002b8b5c14189ad6b4afb52c Mon Sep 17 00:00:00 2001 From: Chaps SD Date: Fri, 13 Apr 2018 10:44:24 -0400 Subject: [PATCH] Adds the download_to_path keyword argument to the download method. Edits smartfile/__init__.py - Adds a download_to_path keyword argument to explicitly specify a path to write the downloaded file contents , if not given the file will be written under the CWD + the file to download name. --- smartfile/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/smartfile/__init__.py b/smartfile/__init__.py index 958e9fa..fe64074 100644 --- a/smartfile/__init__.py +++ b/smartfile/__init__.py @@ -148,7 +148,7 @@ def upload(self, filename, fileobj): arg = (filename, fileobj) return self.post('/path/data/', file=arg) - def download(self, file_to_be_downloaded, perform_download=True): + def download(self, file_to_be_downloaded, perform_download=True, download_to_path=None): """ file_to_be_downloaded is a file-like object that has already been uploaded, you cannot download folders """ response = self.get( @@ -156,10 +156,11 @@ def download(self, file_to_be_downloaded, perform_download=True): if not perform_download: # The caller can decide how to process the download of the data return response - + if not download_to_path: + download_to_path = file_to_be_downloaded.split("/")[-1] # download uses shutil.copyfileobj to download, which copies # the data in chunks - o = open(file_to_be_downloaded, 'wb') + o = open(download_to_path, 'wb') return shutil.copyfileobj(response.raw, o) def move(self, src_path, dst_path):