首先,对冗长的帖子表示抱歉。
我需要将前几天的.xlsx文件从一个文件夹移动到另一个文件夹,但是我应该将具有今天日期的文件,只是前几天的文件移动。
在我当前的代码中,我通过指定每个文件的负号X天来移动文件,但我认为该代码既不是最佳的'形状'也不是最佳的性能/质量
import datetime,os, glob, shutil
from pathlib import Path
dir_carteiras = r"C:\Users\User\Documents\Carteiras"
test = os.listdir(dir_carteiras)
for item in test:
if item.endswith(".jpg"):
os.remove( os.path.join(dir_carteiras, item))
#get todays date and a calculation for today -1 day
today=datetime.date.today()
minus_one_day=datetime.timedelta(days=-1)
#use the calculation to get previous 5 days strings
d_N1=today+minus_one_day
d_N1_ = d_N1.strftime('%Y.%m.%d')
d_N2=d_N1+minus_one_day
d_N3=d_N2+minus_one_day
d_N4=d_N3+minus_one_day
d_N5=d_N4+minus_one_day
#format dates for the files formats
Date_Carteira_N1 = d_N1.strftime('%d_%m_%Y')
Date_Carteira__0 = today.strftime('%d_%m_%Y')
Date_Carteira_N2 =d_N2.strftime('%d_%m_%Y')
Date_Carteira_N3 =d_N3.strftime('%d_%m_%Y')
Date_Carteira_N4 =d_N4.strftime('%d_%m_%Y')
Date_Carteira_N5 =d_N5.strftime('%d_%m_%Y')
Alocacao_0 = today.strftime('%Y%m%d')
Alocacao_N1 = d_N1.strftime('%Y%m%d')
Alocacao_N2 = d_N2.strftime('%Y%m%d')
Alocacao_N3 = d_N3.strftime('%Y%m%d')
Alocacao_N4 = d_N4.strftime('%Y%m%d')
Alocacao_N5 = d_N5.strftime('%Y%m%d')
#folder destination
NewFolderPath = dir_carteiras+"\Historico"
Path(NewFolderPath).mkdir(parents=True, exist_ok=True)
#Move the "Carteira" files from previous 5 days
Carteiras_move_N1 = glob.glob(r'C:\Users\\Documents\Carteiras\*'+Date_Carteira_N1+'.xlsx', recursive=True)
for f in Carteiras_move_N1:
shutil.move(f,NewFolderPath)
Carteiras_move_N2 = glob.glob(r'C:\Users\User\Documents\Carteiras\*'+Date_Carteira_N2+'.xlsx', recursive=True)
for f in Carteiras_move_N2:
shutil.move(f,NewFolderPath)
Carteiras_move_N3 = glob.glob(r'C:\Users\User\Documents\Carteiras\*'+Date_Carteira_N3+'.xlsx', recursive=True)
for f in Carteiras_move_N3:
shutil.move(f,NewFolderPath)
Carteiras_move_N4 = glob.glob(r'C:\Users\User\Documents\Carteiras\*'+Date_Carteira_N4+'.xlsx', recursive=True)
for f in Carteiras_move_N4:
shutil.move(f,NewFolderPath)
Carteiras_move_N5 = glob.glob(r'C:\Users\User\Documents\Carteiras\*'+Date_Carteira_N5+'.xlsx', recursive=True)
for f in Carteiras_move_N5:
shutil.move(f,NewFolderPath)
#Move the "Alocacao" files from the previous 5 days
Alocacao_move_N1 = glob.glob(r'C:\Users\User\Documents\Carteiras\*'+Alocacao_N1+'.xlsx', recursive=True)
for f in Alocacao_move_N1:
shutil.move(f,NewFolderPath)
Alocacao_move_N2 = glob.glob(r'C:\Users\User\Documents\Carteiras\*'+Alocacao_N2+'.xlsx', recursive=True)
for f in Alocacao_move_N2:
shutil.move(f,NewFolderPath)
Alocacao_move_N3 = glob.glob(r'C:\Users\User\Documents\Carteiras\*'+Alocacao_N3+'.xlsx', recursive=True)
for f in Alocacao_move_N3:
shutil.move(f,NewFolderPath)
Alocacao_move_N4 = glob.glob(r'C:\Users\User\Documents\Carteiras\*'+Alocacao_N4+'.xlsx', recursive=True)
for f in Alocacao_move_N4:
shutil.move(f,NewFolderPath)
Alocacao_move_N5 = glob.glob(r'C:\Users\User\Documents\Carteiras\*'+Alocacao_N5+'.xlsx', recursive=True)
for f in Alocacao_move_N5:
shutil.move(f,NewFolderPath)
使用泰勒的答案进行编辑:
import os
import shutil
from datetime import datetime
dir_carteiras = r"C:\Users\GuilhermeMachado\Documents\Carteiras"
test = os.listdir(dir_carteiras)
NewFolderPath = dir_carteiras+"\Historico"
datetime_object = datetime.now()
today = datetime_object.day
month = datetime_object.month
year = datetime_object.year
for file in list(filter(os.path.isfile, os.listdir(dir_carteiras))):
unix_time = os.path.getmtime(file)
date = datetime.utcfromtimestamp(unix_time)
date_day = int(date.strftime("%d"))
date_month = int(date.strftime("%m"))
date_year = int(date.strftime("%Y"))
if date_day < today or date_month < month or date_year < year:
shutil.move(file, NewFolderPath)
我认为您正在追寻类似的东西?
import os
import shutil
from datetime import datetime
datetime_object = datetime.now()
today = datetime_object.day
month = datetime_object.month
year = datetime_object.year
for file in list(filter(os.path.isfile, os.listdir())):
unix_time = os.path.getmtime(file)
date = datetime.utcfromtimestamp(unix_time)
date_day = int(date.strftime("%d"))
date_month = int(date.strftime("%m"))
date_year = int(date.strftime("%Y"))
if date_day < today or date_month < month or date_year < year:
shutil.move(file, NewFolderPath)