1

I tried to save an excel file in azure databricks with a dynamic name:

    import pandas as pd
    
    #initialize the excel writer
    writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
    
    #store your dataframes in a  dict, where the key is the sheet name you want
    frames = {'sheet_1': df_1, 'shet_2': df_2,
            'sheet_3': df_3}
    
    #now loop thru and put each on a specific sheet
    for sheet, frame in  frames.items(): # .use .items for python 3.X
        frame.to_excel(writer, sheet_name = sheet)
    
    #critical last step
    writer.save()

Next I did it:

%sh
sudo mv test.xslx /dbfs/mnt/

It works but I would like to add the date to the name of the file:

test_2= 'test' + datetime.today().strftime("%d%m%y") + '.xlsx'

But I don't know how to do it with %sh

jos97
  • 405
  • 6
  • 18

1 Answers1

0

if you have only one file in that directory, then just do

mv test*.xlsx /dbfs/mnt/....

or you can use the date command to format date:

mv test$(date +'%d%m%Y').xlsx /dbfs/mnt/....
Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • Thank you, I tried but got: mv: cannot stat 'test02092021.xlsx': No such file or directory. I guess it is when I do writer at the beginning – jos97 Sep 02 '21 at 16:17
  • 1
    yes, you need to have the dynamic name in the python code. Or you can do: `mv test.xlsx /dbfs/mnt/..../test$(date +'%d%m%Y').xlsx` <- keep the name in the Python static, but rename on the moving. – Alex Ott Sep 02 '21 at 16:26