简体   繁体   English

按创建/修改日期移动文件,然后使用Python移动

[英]Moving Files by creation/modification date then moving with Python

I am new to programming, even more so with Python. 我是编程的新手,Python更是如此。 So please excuse any ignorance on my part. 因此,请原谅我的无知。 I am trying to write a script for myself that will move files that have been modified in the last 24 hours. 我正在尝试为自己编写一个脚本,该脚本将移动最近24小时内已修改的文件。 So far I have came up with this: 到目前为止,我已经想到了这一点:

import datetime
import os
import shutil


src = "C:\Users\Student\Desktop\FolderA"
dst = "C:\Users\Student\Desktop\FolderB"


now = dt.datetime.now()
before = now - dt.timedelta(hours=24)

def mins_since_mod(fname):
    return (os.path.getmtime(fname))


for fname in os.listdir(src):
    if mins_since_mod > before:
        src_fname = os.path.join(src,fname)
        os.path.join(dst,fname)
        shutil.move(src_fname, dst)

I know i'm close to the solution, but I can't seem to figure out how to get this to work. 我知道我已经接近解决方案了,但是我似乎无法弄清楚如何使它起作用。 I looked around here on the community and was not able to find a solution to my problem. 我在社区的四处张望,却找不到解决我问题的方法。 Thank you for any leads or suggestions. 感谢您提供任何线索或建议。

Hey mate I have actually just done something like this myself. 嗨,老兄,我实际上只是自己做过这样的事情。 I found that there will be a few issues will the time comparison as well as some issues in comparing and moving folders. 我发现时间比较将存在一些问题,而比较和移动文件夹将存在一些问题。

Try this: 尝试这个:

import os
import shutil
import datetime

def filter_by_date(src_folder, archive_date):
    os.chdir(src_folder)
    delay_time = 24 * 60 * 60
    archive_period = archive_date - delay_time
    return [
        name for name in os.listdir(u'.')
        if os.path.isdir(name)
        and datetime.datetime.fromtimestamp(os.path.getmtime(name)) < archive_period
    ]


if __name__ == '__main__':
    folders = filter_by_date("C:/Users/Student/Desktop/FolderA", time.time())
    for files in folders:
        print files
        try:
            shutil.copytree(files, os.path.join("C:/Users/Student/Desktop/New", files))
        except OSError as e:
            print('\nDirectory not copied. Error: %s' % e)
        except shutil.Error as e:
            try:
                files = files.encode('UTF-8')
                dst_path = os.path.join('C:/Users/Student/Desktop/FolderB/', files)
                shutil.copytree(files, dst_path)
            finally:
                print('\nDirectory not copied. Error: %s' % e)

    print "\Completed"

This is going to ensure any file name (including Chinese, Russian and Japanese will be copied) and any folder (directory or sub-directory) is copied. 这将确保复制任何文件名(包括中文,俄语和日语)和所有文件夹(目录或子目录)。 It will also keep all file attributes. 它还将保留所有文件属性。

There are a few things to change. 有几件事需要更改。 First, you can't compare the datetime in before to the Unix timestamp that getmtime() returns. 首先,您无法将beforedatetime时间与getmtime()返回的Unix时间戳进行比较。 It's easier to just use that directly. 直接使用它会更容易。 Also, you actually need to pass the (full) filename to mins_since_mod() for it to do anything. 同样,您实际上需要将(完整)文件名传递给mins_since_mod()才能执行任何操作。

Here's something that should work, changing the name of mins_since_mod() to reflect what it does better: 这是应该起作用的方法,更改mins_since_mod()的名称以反映它的更好之处:

import time
import os
import shutil

SECONDS_IN_DAY = 24 * 60 * 60

src = "C:\Users\Student\Desktop\FolderA"
dst = "C:\Users\Student\Desktop\FolderB"

now = time.time()
before = now - SECONDS_IN_DAY

def last_mod_time(fname):
    return os.path.getmtime(fname)

for fname in os.listdir(src):
    src_fname = os.path.join(src, fname)
    if last_mod_time(src_fname) > before:
        dst_fname = os.path.join(dst, fname)
        shutil.move(src_fname, dst_fname)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM