【发布时间】:2017-04-22 22:51:10
【问题描述】:
我正在尝试创建一个脚本,它将仅将过去 24 小时内的新文件或更新文件移动到新文件夹中。到目前为止,我创建了一个可以移动文件的脚本,任何线索或建议将不胜感激。
import os, shutil
source = os.listdir('C:\Users\Student\Desktop\FolderA')
destination = 'C:\Users\Student\Desktop\FolderB'
os.chdir('C:\Users\Student\Desktop\FolderA')
for files in os.listdir("C:\Users\Student\Desktop\FolderA"):
if files.endswith(".txt"):
src = os.path.join("C:\Users\Student\Desktop\FolderA",files)
dst = os.path.join(destination,files)
shutil.move(src,dst)
【问题讨论】:
-
这里有一个问题可以帮助您获取文件的创建日期:stackoverflow.com/questions/237079/… 有了创建日期,您可以将它们与当前日期进行比较。
-
(1) 不要在同一代码中重复你的硬编码文字
FolderA路径 4 次; (2) 使用os.stat(filename).st_mtime获取文件的最后修改时间戳。
标签: python