Update data file from server code

What I’m trying to do:
Hi everyone. I am trying to update (append to) a data file that i have uploaded using the server module. I am unsure exactly what the correct procedure is for editing and overwriting the existing file or if its even possible. My main question here is: what would be the best way to do this and how?
What I’ve tried and what’s not working:
My process is currently as follows:

  • User uploads a file that the server reads and edits after converting to a pandas data frame.
  • The server takes the edited file and appends the data to the end of the existing data file (located on the datafiles) converted into a pandas dataframe.
  • Next i want to overwrite or update the old data file using the newly created pandas dataframe.

The last point is where i get stuck. Should i be following a different process to achieve this or is there a way to code this in the anvil server module?
Thanks in advance!
Code Sample:

  df = process_data(f) # User defined function - returns cleaned pandas data frame
  df_legacy = pd.read_csv(data_files['old_data.csv'], index_col=0)
  df_legacy = df_legacy[df.columns]
  # Concat new processed data with the legacy data
  df_new = pd.concat([df_legacy, df])
  data_files['old_data.csv'] = df_new.to_csv() # Trying to overwrite file - Unsure of correct code or process

**Clone link:**
*share a copy of your app*

Hi @anvil,

You can write to data files but, if it’s a file that’s being updated a lot, then it isn’t recommended to use Data Files as a solution due to a lack of concurrency control. See here for information on how to update files and information on alternatives for files that update regularly:

Thanks for the response. Is it maybe better to create a new file in the data files from my data frame and delete the old one? If so, what would be the best method?

That would still count as

wouldn’t it? Just by a more roundabout method.

If it’s being updated regularly, why use a file at all, when a database, designed for regular updates, is at hand?

1 Like

I will try it that way thank you for your input.