如果文件存在,则创建一个备份文件+附加日期时间+移动文件。蟒蛇 [英] Making a backup file + appending date time + moving file if the file exists. PYTHON

查看:143
本文介绍了如果文件存在,则创建一个备份文件+附加日期时间+移动文件。蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的工作,但我拼在一起的努力。



我想把一个文件,移动到一个备份文件夹,从这个文件中获取日期时间,然后把它附加到文件名/改变文件名到文件名+日期时间。

这部分以我想要的格式获取日期时间。 (打印行是格式正确的日期时间,但我不需要打印这一行)

pre $ Filepath =C: \\ SyncWork \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ path.getmtime(Filepath)

print(datetime.fromtimestamp(modifiedTime).strftime(%b-%d-%y-%H:%M:%S))

这部分会重新命名/移动文件(但缺少日期时间)

  prevName ='c:\\syncwork\\\\\\\\\\\\\\\\\\\\\\\\\' b newName ='c:\\syncwork\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' (prevName,newName)

如何将打印行与我喜欢的格式转换为字符串并将其附加到newName li的末尾NE?



解答我的问题之后
我的最终代码如下所示:


修改时间= os.path.getmtime(Filepath)



timestamp = datetime.fromtimestamp(modifiedTime).strftime(%b-%d-%Y_%H.%M 。%S)

prevName ='c:\\SyncWork\\\ACE\\Files\\ESAL_P\\ESAL_P.txt'
newName ='c:\\SyncWork\\ACE\\Files\\ESAL_P\\Backup\\ESAL_P'

os.rename(prevName, newName +_+ timestamp +.txt)
print(newName)


解决方案

我刚刚测试了一个名为temp的文件,该文件被改为temp_Sep-15-14-08:42:57

  FilePath ='temp'#用你的文件路径/名称替换temp 
modifie dTime = os.path.getmtime(FilePath)

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


I have a few of the pieces of this working but I'm struggling with putting them together.

I would like to take a file, move it to a backup folder, get the date time from that file, and append it to the file's name / change the file's name to file name + date time.

This part gets the date time in the format I want it. (print line is the date time formatted properly, but I don't need to print this line)

    Filepath = "C:\\SyncWork\\ACE\\Files\\ESAL_P\\ESAL_P.txt"
    modifiedTime = os.path.getmtime(Filepath) 
    firstFile = os.path.getmtime(Filepath)

    print (datetime.fromtimestamp(modifiedTime).strftime("%b-%d-%y-%H:%M:%S"))

This part will rename / move the file (But it's missing the datetime)

    prevName = 'c:\\syncwork\\ace\\files\\ESAL_P\\ESAL_P.txt'
    newName = 'c:\\syncwork\\ace\\files\\ESAL_P\\Backup\\ESAL_P.txt'

    os.rename(prevName, newName)

How do I turn the print line with the formatting that I like into a string and append it to the end of the newName line?

AFTER my question was answered My final code looked like this:

Filepath = "C:\\SyncWork\\ACE\\Files\\ESAL_P\\ESAL_P.txt"
modifiedTime = os.path.getmtime(Filepath) 



timestamp = datetime.fromtimestamp(modifiedTime).strftime("%b-%d-%Y_%H.%M.%S")

prevName = 'c:\\SyncWork\\ACE\\Files\\ESAL_P\\ESAL_P.txt'
newName = 'c:\\SyncWork\\ACE\\Files\\ESAL_P\\Backup\\ESAL_P' 

os.rename(prevName, newName+"_"+timestamp + ".txt")
print(newName)  

解决方案

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)

这篇关于如果文件存在,则创建一个备份文件+附加日期时间+移动文件。蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆