How to append a dataframe to a csv or excel file on streamlit without overwriting the existing file

What are the possible ways one can append a dataframe to an existing csv or excel files on streamlit without overwriting the existing file.

I have a function that does this on a jupyter notebook, but I want to integrate that into my streamlit app.

The function simply checks if the given csv file already exist, if it does, it appends the new Dataframe to it. but if it doesn’t exist, it will create the csv file

file = 'my_csv.csv'
import os
def appendDFToCSV_void(df, csvFilePath, sep=",",encoding='utf-8'):
    try:
        if not os.path.isfile(csvFilePath):
            df.to_csv(csvFilePath, mode='a', index=False, sep=sep)
        elif len(df.columns) != len(pd.read_csv(csvFilePath, nrows=1, sep=sep).columns):
            raise Exception("Columns do not match!! Dataframe has " + str(len(df.columns)) + " columns. CSV file has " + str(len(pd.read_csv(csvFilePath, nrows=1, sep=sep).columns)) + " columns.")
        elif not (df.columns == pd.read_csv(csvFilePath, nrows=1, sep=sep).columns).all():
            raise Exception("Columns and column order of dataframe and csv file do not match!!")
        else:
            df.to_csv(csvFilePath, mode='a', index=False, sep=sep, header=False)
    except PermissionError:
        pass

It shouldn’t be any different. Streamlit code is run on the server, which includes all i/o operations. So, on your local machine, the server runs and saves files relative to the app root folder. In Streamlit sharing (online) however, it’s not a good idea to save files in the deployed container as they are ephemeral. See this post for more info.

HTH,
Arvindra

1 Like

Thanks