How do you add datetime to a logfile name?

后端 未结 7 655
孤街浪徒
孤街浪徒 2020-12-29 19:42

When I create my logfile, I want the name to contain the datetime.

In Python you can get the current datetime as:

>>> from datetime import da         


        
7条回答
  •  情书的邮戳
    2020-12-29 19:59

    Another Solution using format():

    #generates a date for a generic filename
    import datetime
    date_raw = datetime.datetime.now()
    date_processed = "{}-{}-{}_{}-{}-{}".format(date_raw.year, date_raw.month, 
                      date_raw.day, date_raw.hour, date_raw.minute, date_raw.second)
    #example value: date_processed = 2020-1-7_17-17-48
    

    I used this in my own project
    edit: as I found out about f(ormatted)-strings, this would be another solution:

    date_processed = f"{date_raw.year}-{date_raw.month}-{date_raw.day}_{date_raw.hour}-{date_raw.minute}-{date_raw.second}"
    

提交回复
热议问题