Making a backup file + appending date time + moving file if the file exists. PYTHON

社会主义新天地 提交于 2019-12-06 20:28:28

I just tested the following on a file named "temp" which was changed to "temp_Sep-15-14-08:42:57"

FilePath = 'temp' # replace the temp with your file path/name
modifiedTime = os.path.getmtime(FilePath) 

timeStamp =  datetime.datetime.fromtimestamp(modifiedTime).strftime("%b-%d-%y-%H:%M:%S")
os.rename(FilePath,FilePath+"_"+timeStamp)

This should do it:

timestamp = (datetime.fromtimestamp(modifiedTime).strftime("%b-%d-%y-%H:%M:%S"))
newName = 'c:\\syncwork\\ace\\files\\ESAL_P\\Backup\\ESAL_P.txt.' + timestamp
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!