Search code examples
pythonpandascsvexport-to-csvdropbox-api

Upload a csv to DropBox from Stream


I am using the below code to save a Pandas Dataframe into a xlsx file and upload it to Dropbox without saving it locally on my machine.

Is it possible to do so with df.to_csv? I cannot find any information on how to use the same method, of not saving it locally and pushing it into Dropbox straight away.

DBX = dropbox.Dropbox('TOKEN')
with io.BytesIO() as stream:

    with pd.ExcelWriter(stream) as writer:
        df.to_excel(writer, index=False)
        writer.save()

    stream.seek(0)

    DBX.files_upload(stream.getvalue(), "/test.xlsx", mode=dropbox.files.WriteMode.overwrite)

Solution

  • Below code works:

    csv_buffer = StringIO()
    df_new.to_csv(csv_buffer, index=False)
    
    DBX.files_upload(df_new.to_csv(index=False).encode(), "/TEST.csv", mode=dropbox.files.WriteMode.overwrite)