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

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

    now is a class method in the class datetime in the module datetime. So you need

    datetime.datetime.now()
    

    Or you can use a different import

    from datetime import datetime
    

    Done this way allows you to use datetime.now as per the code in the question.

提交回复
热议问题