0

I'm successfully able to download CSV files from a folder on Exavault, using the files provided by Exavault, but the download gets saved in a temporary folder on my Mac.

How do I define the directory to save it to? This is an excerpt of the script.

resources_api = ResourcesApi()
resources_api.api_client.configuration.host = ACCOUNT_URL


  try:
        list_result = resources_api.list_resources(
        API_KEY, ACCESS_TOKEN, "/files", offset=0, type='file', name='*.csv')

    if list_result.returned_results == 0:
        print("Found no files to download")
        sys.exit(0)
    else:
        print("Found {} CSV files to download".format(list_result.returned_results))

except Exception as e:
    raise e
    print('Exception when calling Api:', str(e))
    sys.exit(1)

downloads = []
listed_files = list_result.data
for listed_file in listed_files:
    downloads.append("id:{}".format(listed_file.id))
    print(listed_file.attributes.path)

try:

    downloaded_file = resources_api.download(API_KEY, ACCESS_TOKEN, downloads, download_archive_name="sample-csvs")

    print("File(s) downloaded to", os.path(downloaded_file))

Separately there's a resources_api.py file, which might hold the answer, but if I edit that, I'm not sure how I would invoke the changes.

Any help would be appreciated.

Steven Diffey
  • 73
  • 1
  • 7
  • Well, yes; it's `resources_api.download` that decides the location. If that's a file you control, you'll have to scroll through the source to figure out how it makes that decision. – Tim Roberts Jul 23 '22 at 06:20
  • Thanks for the speedy response! Would I have to reinstall the library to invoke it, considering it's not directly linked to the above script? – Steven Diffey Jul 23 '22 at 06:26
  • Nothing in Python is "directly linked". The only source for modules is the source. If you change the source, it changes it for all users. – Tim Roberts Jul 23 '22 at 06:28
  • Got it. Should I go along the lines of this to do that then? https://stackoverflow.com/questions/23075397/python-how-to-edit-an-installed-package – Steven Diffey Jul 23 '22 at 06:31

1 Answers1

0

When looking at the code of the API, you can see that it is written such that it always creates a temporary folder for you:

# Write the file to the temporary folder configured for our client
fd, path = tempfile.mkstemp(dir=self.api_client.configuration.temp_folder_path)

You could check if you can try to change api_client.configuration.temp_folder_path and see if that works for you.

Or even better, just copy the file to a location of your choice. You can do it with shutil

import shutil
import os

targetFolder= "/your/folder/"
filename = os.path.basename(downloaded_file)
destination = os.path.join(targetFolder,filename )
shutil.copy(downloaded_file, destination)
user_na
  • 2,154
  • 1
  • 16
  • 36