Python Forum
Naming the file as time and date. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Naming the file as time and date. (/thread-32013.html)



Naming the file as time and date. - BettyTurnips - Jan-14-2021

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')



RE: Naming the file as time and date. - ndc85430 - Jan-15-2021

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.


RE: Naming the file as time and date. - buran - Jan-15-2021

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")



RE: Naming the file as time and date. - BettyTurnips - Jan-15-2021

Thanks for the feedback. I'll have a good read up and give it another go.