Skip to content
Advertisement

Defining path for downloaded CSV file in python3

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.

Advertisement

Answer

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 contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement