Adding timestamp to a file in PYTHON

前端 未结 2 662
我在风中等你
我在风中等你 2021-01-15 09:43

I can able to rename a file without any problem/error using os.rename().

But the moment I tried to rename a file with timestamp adding to it, it throws win3 error or

2条回答
  •  终归单人心
    2021-01-15 10:12

    Your timestamp format has colons in it, which are not allowed in Windows filenames. See this answer on that subject:

    How to get a file in Windows with a colon in the filename?

    If you change your timestamp format to something like:

    timestamp = str(now.strftime("%Y%m%d_%H-%M-%S"))
    

    it should work.

提交回复
热议问题