复制创建日期在特定范围内的文件

Copy files with their creation date in a specific range

'''我目前正在尝试使用 shutil 将文件从一个文件夹复制到另一个文件夹,但我似乎无法让它工作,该过程说它已完成但没有任何反应?'''

我添加的当前条件 raw_input 让用户可以选择文件扩展名。 我要添加的下一个条件是日期范围函数,因此我可以选择一个日期范围,例如: 17/07/2020 至 04/08/2020 或当天的日期。

*更新代码

import os
import shutil
import os.path, time
from pip._vendor.distlib.compat import raw_input

os.chdir('C://')
src = ("C:/Users/eldri/OneDrive/Desktop/")
dst = ("C:/Users/eldri/OneDrive/Desktop/output")
ext = raw_input("[+] File format: ")
created = (" last modified: %s" % time.ctime(os.path.getmtime(src)))
start = raw_input("[+] Date start: ")
end = raw_input("[+] Date end: ")

def date_to_num(date):
    return int("".join(date.split('/')[::-1]))

def date_in_range(date, start, end):
    return date_to_num(date) > date_to_num(start) and date_to_num(date) < date_to_num(end)

for filename in os.listdir(src):
    if filename.endswith('.'+ext) and created.startswith(start) and created.endswith(end):
        shutil.copy( src + filename, dst)
        print("[+] File transferred "+filename + created)
    else:
        print("[+] File not transferred "+filename + created)
        
print("[+] Transfer complete")

我在看 pandas?但不确定 python.

还是很新
example on terminal
file extension = .csv
startdate = 12/05/2020
enddate = 07/08/2020

一旦用户输入了这些字段,它将只复制所需的文件。

创建文件的当前输出是:

[+] File transferred BASE1011.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1112.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1213.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1314.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1415.xls last modified: Fri Jul 17 10:11:40 2020

我希望这些格式更易于用户输入,如上所述: 示例:开始 12/05/2020 结束日期 = 07/08/2020

谢谢你的帮助,我不是最擅长的 python 但我正在努力学习所以任何帮助都会很棒。

谢谢

对于特定范围你可以:

创建一个将日期解析为数字的函数:

def date_to_num(date):
    return int("".join(date.split('/')[::-1]))

def date_in_range(date, start, end):
    return date_to_num(date) > date_to_num(start) and date_to_num(date) < date_to_num(end)

然后像这样使用它:

date_in_range("03/02/2020", "01/01/2020", "05/05/2020")

我使用 time.ctime(os.path.getmtime(src)) 继续你的工作并创建了一个函数 dateRange(createdDate, startDate, endDate),它使用 datetime 将字符串转换为日期时间对象和 returns True 或 False如果创建日期介于开始日期和结束日期之间

import os
import shutil
import time
from datetime import datetime

src = "C:/Users/eldri/OneDrive/Desktop/"
dst = "C:/Users/eldri/OneDrive/Desktop/output"
ext = input("[+] File format: ")  # "txt"
start = input("[+] Date start: ")  # "01/07/2020"
end = input("[+] Date end: ")  # "30/07/2020"


def dateRange(createdDate, startDate, endDate):
    """determines if date is in range"""
    createdDate = datetime.strptime(createdDate, '%a %b %d %H:%M:%S %Y')
    startDate = datetime.strptime(startDate, '%d/%m/%Y')
    endDate = datetime.strptime(endDate, '%d/%m/%Y')
    return startDate < createdDate < endDate


for filename in os.listdir(src):
    created = time.ctime(os.path.getmtime(src + filename))
    if filename.endswith('.' + ext) and dateRange(created, start, end):
        shutil.copy(src + filename, dst)
        print("[+] File transferred " + filename + created)
    else:
        print("[+] File not transferred " + filename + created)

print("[+] Transfer complete")

我在 extstartend 变量的末尾添加了示例,以提供格式的概念