How do you add datetime to a logfile name?

后端 未结 7 637
孤街浪徒
孤街浪徒 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 20:05

    You need datetime.strftime(), this allows you to format the timestamp using all of the directives of C's strftime(). In your specific case:

    >>> datetime.now().strftime('mylogfile_%H_%M_%d_%m_%Y.log')
    'mylogfile_08_48_04_02_2012.log'
    

提交回复
热议问题