Python Forum

Full Version: Naming the file as time and date.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a script that scrapes web data then puts it into an excel file and names it "Results". I'm looking to get the file name to show the current time and date. I'm very new to learning to code and I've had a good go at it myself but I cant work it out without getting errors. Any help would be greatly appreciated.

markets = main("10932509, 7129730, 35, 37, 117, 81, 59, 55, 9404054")
#print(markets)

df = pd.DataFrame(markets)
df.to_excel('Results.xlsx')
Have a look at the facilities provided by the datetime module: https://docs.python.org/3/library/datetime.html, specifically the datetime class and the following section: https://docs.python.org/3/library/datetime.html.

If you're having problems and getting errors, you need to show all the relevant code and the full traceback.
You may look also at pandas.Timestamp.now

e.g. check

import pandas as pd
print(pd.Timestamp.now().strftime('%Y_%m_%d_%H_%M_%S'))
print(f"{pd.Timestamp.now().strftime('%Y_%m_%d_%H_%M_%S')}.xlsx")
Thanks for the feedback. I'll have a good read up and give it another go.