Adding timestamp to a file in PYTHON

前端 未结 2 661
我在风中等你
我在风中等你 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.

    0 讨论(0)
  • 2021-01-15 10:15

    you can't have : as part of the filename

    change

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

    to

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

    and you'll be able to rename your file

    0 讨论(0)
提交回复
热议问题