Discover this year's submissions to the Dataiku Frontrunner Awards and give kudos to your favorite use cases and success stories!READ MORE

Python to Export File to Local Drive

Solved!
Pete1
Level 1
Python to Export File to Local Drive

Is there a way I can take a file (.csv or .xlsx) from a folder in DSS and send it to a local folder using Python?


Operating system used: Windows

0 Kudos
1 Solution
CatalinaS
Dataiker
Dataiker

Hi @Pete1 ,

Below is an example Python code that uses Dataiku Python APIs to export the files from a DSS folder to a local folder called output :

import dataikuapi
import io, os

host="http://localhost:11200"
apiKey = "*******"
project_key="******"
folder_id="*****"

def api_py_function(project_key, folder_id):
    client = dataikuapi.DSSClient(host,apiKey)
    folder = dataikuapi.dss.managedfolder.DSSManagedFolder(client, project_key, folder_id)
    contents = folder.list_contents()
    if not os.path.exists("output"):
        os.mkdir("output")
    for item in contents["items"]:
        with open("output/"+item["path"], "wb") as f:
                file = folder.get_file(item["path"])
                f.write(file.content) ​

api_py_function(project_key,folder_id) 

 

View solution in original post

1 Reply
CatalinaS
Dataiker
Dataiker

Hi @Pete1 ,

Below is an example Python code that uses Dataiku Python APIs to export the files from a DSS folder to a local folder called output :

import dataikuapi
import io, os

host="http://localhost:11200"
apiKey = "*******"
project_key="******"
folder_id="*****"

def api_py_function(project_key, folder_id):
    client = dataikuapi.DSSClient(host,apiKey)
    folder = dataikuapi.dss.managedfolder.DSSManagedFolder(client, project_key, folder_id)
    contents = folder.list_contents()
    if not os.path.exists("output"):
        os.mkdir("output")
    for item in contents["items"]:
        with open("output/"+item["path"], "wb") as f:
                file = folder.get_file(item["path"])
                f.write(file.content) ​

api_py_function(project_key,folder_id)