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

后端 未结 6 1374
北海茫月
北海茫月 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:32

    While not using datetime, this solves your problem (answers your question) of getting a string with the current time and date format you specify:

    import time
    timestr = time.strftime("%Y%m%d-%H%M%S")
    print timestr
    

    yields:

    20120515-155045
    

    so your filename could append or use this string.

提交回复
热议问题