0

I have function in Python Pandas like below (it is sample):

def xyz():
    df = pd.DataFrame({"a" : [1,1,1]})
    TodaysDate = time.strftime("%Y-%m-%d")
    excelfilename = "raport_" +TodaysDate +".xlsx"
    df.to_excel(excelfilename, sheet_name="raport", index=True)

    return df

Every time I run above function the existing excel file gets overwritten but I need a new excel file to be created every time I run the function. How can I modify my function to do that in Python ?

5 Answers 5

1

Maybe something like this. Depends on how many excel files you want to generate.

import random
excelfilename = "raport_" + str(random.randrange(9999)) +TodaysDate +".xlsx"
Sign up to request clarification or add additional context in comments.

Comments

1

You can change TodaysDate = time.strftime("%Y-%m-%d") to TodaysDate = str(time.strftime("%Y-%m-%d %X")).replace(":", "") or TodaysDate = str(TodaysDate.strftime("%Y-%m-%d %H%M%S"))

This will give you an additional Hour/Minute/Seconds for the creation of your excel. So unless you are running this function multiple times a second this should cover your needs.

4 Comments

Unfortunately it does not work, I have error: OSError: [Errno 22] Invalid argument: 'raport_2022-06-01_16:23:52.xlsx'
DO you have other idea ? :)
You can replace the ":" by this - TodaysDate = str(time.strftime("%Y-%m-%d %X")).replace(":", "")
Oh yes @vovakirdan method will work, I've also updated the code to reflect this idea without the replace (whichever you prefer)
0

So you can do in this way:

def xyz(itr):
    df = pd.DataFrame({"a" : [1,1,1]})
    TodaysDate = time.strftime("%Y-%m-%d")
    excelfilename = "raport_" +TodaysDate + str(itr) + ".xlsx"
    df.to_excel(excelfilename, sheet_name="raport", index=True)

    return df

for i in range(9): #or smth another iteration
    xyz(i)

Comments

0

You should really use ArchAngelPwn's solution in my opinion. Applying random numbers might work but you still run the 1/1000 chance that one file will get overwritten. Also, you might not know which file belongs to which loop/run.

You could also save your file names in a list and check if it exists :

fn = []
def xyz():
    df = pd.DataFrame({"a" : [1,1,1]})
    TodaysDate = time.strftime("%Y-%m-%d")
    excelfilename = "raport_" +TodaysDate +".xlsx"
    temp = len(np.where((np.array(fn) == excelfilename)))
    fn.append(excelfilename)
    df.to_excel("raport_" +TodaysDate + "_" + temp + ".xlsx", sheet_name="raport", index=True)

return df

Comments

0

Its because % in your excelfilename becase you are using %X.

Just change:

TodaysDate = time.strftime("%Y-%m-%d %X")

To:

TodaysDate = time.strftime("%Y-%m-%d %H_%M_%S")

I have double checked and its worked on my side :))

For full code:

import pandas as pd
import time


def xyz():
    df = pd.DataFrame({"a": [1, 1, 1]})
    TodaysDate = time.strftime("%Y-%m-%d %H_%M_%S")
    excelfilename = "raport_" + TodaysDate + ".xlsx"
    df.to_excel(excelfilename, sheet_name="raport", index=True)

    return df


xyz()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.