i have created a notebook that opens an xlsx file, does some cleanup, then saves it as a csv. Everything works swimmingly when i hardcode paths; i’ve worked out how to get user input for the filename to concatenate on the end of that path in opening the file. Saving the csv is just a matter of the hardcoded path with a filename based on a value from the file.
However, i really want this to work without hardcoding paths. The only suggestion i’ve found when searching how to get Python to get user input for opening/saving files was to use tkinter
.
So i’ve gotten this code to work (sorry, i comment my code up the wazoo so i don’t forget):
## for opening the xls
import openpyxl as op
## for open file dialog to choose xls
from tkinter import filedialog as fd
## for manipulating the xls as a dataframe
import pandas as pd
## for parsing dates from the xls
from datetime import datetime
## - using tkinter openfile dialog
invoice=pd.read_excel(fd.askopenfilename())
invoice
And this works (except for a strange UserWarning that gets thrown that doesn’t seem to impact the processing of the file).
However, when i go to save the file, the kernel crashes:
## export to csv
invNum=invNum+".csv"
# - using tkinter saveas dialog
savePath=fd.askdirectory()
i’ll get an alert that the kernel has appeared to have died, it will restart momentarily, a blank doc labelled ‘python’ will appear in the dock (inactive), but i have to manually restart the kernel. (i just got this crash running the first set of code above, so it seems to be that the first call to tkinter will go, but subsequent calls will kill the kernel. (i restart and clear output every time.)
in testing this for accuracy for this post, i hardcoded the filepath for opening the file, then called tkinter.askdirectory() - it displayed the dialog correctly and accepted the Save click, but now i have that blank Python doc icon as active in the doc, and the Force Quit Applications dialog shows it as (not responding).
Has anyone seen this, and if so, am i missing something here? i’ve even tried all this after logging out of all python and Jupyter Notebook, quitting Anaconda, and rebooting. Whilst it’s nice to have the askopenfilename(), it’s not useful without the askdirectory() on the other side.
Mac MBA (M1, 2020), OS 12.6.5 Monterey, Anaconda Navigator 2.4.0 (just installed!), Jupyter Notebook 6.5.2, Python 3.10.9, openpyxl 3.0.10, pandas 1.5.3, tk 8.6.12 (all this came with the Anaconda Navigator install).
Thank you!