How to create a file name with the current date & time in Python?

后端 未结 6 1363
北海茫月
北海茫月 2020-12-07 07:40

Here is a functional code (create file with success)

sys.stdout = open(\'filename1.xml\', \'w\')

Now I\'m trying to name the file with the cu

6条回答
  •  眼角桃花
    2020-12-07 08:36

    This one is much more human readable.

    from datetime import datetime
    
    datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
    
    '2020_08_12-03_29_22_AM'
    

提交回复
热议问题