Forums

Unable to save Pandas Dataframe to .txt and .xlsx (No Errors)

I am trying to save a pandas dataframe to a specified path, but no file is returned. I am able to do this when I host the website locally, but when I use PythonAnywhere, the website works fine, but does not save the files. There are no reported errors, so I am not sure what is going wrong.

writer = pd.ExcelWriter('D:\\' + file_title + '.xlsx')
clean_data.to_excel(writer, 'Sheet1', header=False, index=False)
clean_data.to_csv('D:\\' + file_title + '.txt', sep='\t', header=False, index=False)

Do I need to take a different approach to this? Any help is appreciated.

The filenames you're using are Windows-format -- PythonAnywhere is based on Linux, so you'll need to use a Linux-style path. The main difference -- there's no driver letter, and it uses forward slashes ("/") rather than backslashes ("\"). You need to save inside your home directory, so a path like this should work:

"/home/originalang/" + file_title + '.txt'

Thank you for your response. Does this mean that I cannot use python anywhere to save files to a user's local drive? If this is a possibility, what is the correct way to do this? Thank you

Sure, you can provide a download link so that the user can download the file.