My code works, but it looks very bad.
I need to move .xlsx
files from the previous days from one folder to another, but I should'nt move the files with today's date, just the files from previous days.
In my current code, I am moving the files by specifying each one of them with a Minus X days, but I think the code is not in the best 'shape' nor the best performance/quality.
Any one can help me improve it?
import datetime,os, glob, shutil
from pathlib import Path
dir_carteiras = r"C:\Users\GuilhermeMachado\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\GuilhermeMachado\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\GuilhermeMachado\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\GuilhermeMachado\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\GuilhermeMachado\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\GuilhermeMachado\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\GuilhermeMachado\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\GuilhermeMachado\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\GuilhermeMachado\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\GuilhermeMachado\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\GuilhermeMachado\Documents\Carteiras\*'+Alocacao_N5+'.xlsx', recursive=True)
for f in Alocacao_move_N5:
shutil.move(f,NewFolderPath)