Move files depending on their date in Python [closed]

-1

How can I move / copy a file that contains ("YYMMDD file") to its respective folder that contains the name of the Month (January, February, etc.) in Python?

Currently the date code is concatenated with a bday so that it only takes business days:

day = days[0].dt.strftime("%Y%m%d").astype(int)

Then I create an arrangement with the name of the files

files = ['archivo1','archivo2','archivo3',etc...]

I make a for where I send call the calendar with the month and create directories in case of not having them

for month in calendar.month_name[1:]:
    if not os.path.exists(directory+"\"+month):
        os.makedirs(directory+"\"+month)

Then I take the days and in a for I connect it with the names

for x in day :
 for y in files :
  value = str(y)+str(x)
  shutil.copy(directory+"\"+str(y)+".xlsx",directory+"\"+month+"\"+str(value)+".xlsx") //El tema aqui es que cuando lo corro se copia en c/u de los directorios sin tomar en cuenta el mes...

The issue I have is that before the shutil I would like to know if I can send them to the folder with their corresponding month ...

The result that I get with the concatenation is this:

archivo120181212.xlsx

And there it would be to take the month only, I do not know if I have to do it backwards or how ...?

    
asked by Alan 28.12.2018 в 02:13
source

1 answer

1

I made a small script to move the files but I recommend using timestamps because if you print the date in a file it can not be parsed correctly as I do here

from datetime import datetime

time = datetime.now().timestamp()
archivo = "Texto-{}.txt".format(time)
with open(archivo, "w") as f:
    f.write("Hola")

so you can have your file formatted correctly, then retrieve the name in the case of multiple files and then move them with shutil and do a dict with the months     imports     import shutil

meses = {"12":"diciembre"}
for i in os.listdir():
    if 'txt' in i:

    fecha = datetime.utcfromtimestamp(float((nombre.split('-')[1]).replace('.txt','')))


    mestring = meses[str(fecha.month)]

    shutil.move(os.path.join(os.getcwd(), nombre), os.path.join(os.getcwd(), mestring))

and that would be the possible solution to your problem

    
answered by 28.12.2018 в 02:38