I have long analysis during which I need to write the results (tables) to an Excel file. To avoid writing the codes multiple times throughout the analysis, I created function: Its purpose is:
- Check if folder exist or not
- If folder exist, just write the data frame into an excel file into this folder
- If folder not found, create new folder and then write the excel file into it
It used to work but not anymore, now it neither gives an error nor any outputs as if I executed empty cell. I don't understand why, any help is highly appreciated
def dfToExcel(df,filename,sheet_name,index,sub_dir=None):
current_path = os.getcwd()
if sub_dir != None:
path = current_path+'\\'+ sub_dir
ispath = Path(path).exists()
if ispath == False:
os.mkdir(path)
file_path= path+'\\'+ filename
else:
file_path=current_path+'\\'+ filename
ispath = Path(file_path).exists()
if ispath == False:
df.to_excel(file_path ,sheet_name=sheet_name,index=index,engine = 'xlsxwriter')
else:
with pd.ExcelWriter(file_path,mode='a',engine = 'openpyxl') as writer:
df.to_excel(writer,sheet_name=sheet_name, index=index,encoding='iso-8859-1')