Python copy files to a new directory and rename if file name already exists

后端 未结 4 628
梦毁少年i
梦毁少年i 2021-02-02 06:24

I\'ve already read this thread but when I implement it into my code it only works for a few iterations.

I\'m using python to iterate through a directory (lets call it m

4条回答
  •  梦谈多话
    2021-02-02 07:14

    I always use the time-stamp - so its not possible, that the file exists already:

    import os
    import shutil
    import datetime
    
    now = str(datetime.datetime.now())[:19]
    now = now.replace(":","_")
    
    src_dir="C:\\Users\\Asus\\Desktop\\Versand Verwaltung\\Versand.xlsx"
    dst_dir="C:\\Users\\Asus\\Desktop\\Versand Verwaltung\\Versand_"+str(now)+".xlsx"
    shutil.copy(src_dir,dst_dir)
    

提交回复
热议问题