Find files by creation/modification date then moving to another dir in Python

时间:2015-07-29 00:30:51

标签: python python-2.7 move last-modified shutil

First question. I am new to programming, much less python. As the title says I am attempting to find files that were created or modified in the past 24 hours, then move those files to another directory. I can find the files but I can't figure out how to move the files that meet this criteria. My script so far:


for root,dirs,files in os.walk('source\folder'):
for file_name in files: now = dt.datetime.now() before = now - dt.timedelta(hours=24) path = os.path.join(root,file_name) st = os.stat(path)
mod_time = dt.datetime.fromtimestamp(st.st_ctime) if mod_time < before: print('%s modified %s'%(path,mod_time))

I've attempted to use shutil to move the output but I get an error;

TypeError: coercing to Unicode: need string or buffer, datetime.datetime found

I've tried to find a solution online but haven't had any luck. Not even sure if I can do what I am trying to do with the way I have this constructed? Thanks in advance.

1 个答案:

答案 0 :(得分:4)

Instead of:

@Html.ValidationMessageFor()

do:

shutil.move(mod_time, 'dest\path')

This passes that function a filename instead of a date.