-2

I have a python tkinter GUI which will allow me to open a csv or Excel file and work on them and save the result in csv or Excel file .I want to save them in the desired location so I used asksaveasfile filedailog to get save prompt and it's working fine my problem is I want to save the csv or Excel file with the name , extension, location specified in the save prompt .How can I write my csv file to the field object created? Here is the code

def Save(self):
    Self.filename = filedailog.asksavesafile(defaultextension = ".csv")

    Self.filename.write(self.df.to_csv()) 

I want to save the data frame (df) with the specified name and to the specified location and extension

  • 1
    whats the problem with current code? I don't understand what the problem is – LV98 Apr 30 '20 at 08:14
  • Yes it seems to run fine for me – TheFluffDragon9 Apr 30 '20 at 08:25
  • I want to save a csv file .In the above code didn't mentioned name and extension of the file – sai kiran Apr 30 '20 at 09:08
  • Does this answer your question? [writing-a-pandas-dataframe-to-csv-file](https://stackoverflow.com/questions/16923281), [`asksaveasfilename`](https://stackoverflow.com/a/39792020/7414759) – stovfl Apr 30 '20 at 10:08
  • No I want to save the data frame to csv file with name and extension and location specified by the user .How can I get them? – sai kiran Apr 30 '20 at 12:46
  • filedialog.asksaveasfile() returns a file object like you would get with an open() statement. You can the simply just .write() your data to it. If you want the full filepath with extension, you can use filedialog.asksaveasfilename() instead. Does this help? – TheFluffDragon9 Apr 30 '20 at 15:00
  • I found answer for that . From the fileobject.name would return the path and name and extension that I needed ``` – sai kiran Apr 30 '20 at 15:20

1 Answers1

0

I was able get that name , extension and location that user entered from fileobject.name

self.filename = filedailog.asksaveasfile(defaultextension = ".csv")
df.to_csv(filename.name)