Find and copy files from csv file

Long live the blog, this is a little script with python that basically read a .csv file, get a specific row with the name of the file that I needed to find and the copy the file and paste on a diferente folder. Since the origin path start with the first segments of the text row from the .csv file, I modify the script in order to build the path.

So this is the structure of my .CSV file, the row 7 is where the name of the file that I need to find

DataTable to Pivot DataTable

This is the path where I need to find the file

DataTable to Pivot DataTable

source code:

 
import glob, os, csv, shutil

with open('file.csv', 'r') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        date_file = row[7].split("_", 2)
        new_date = date_file[0].split("-", 2)
        hour_array = date_file[1].split("-", 1)
        year = new_date[0]
        month = new_date[1]
        day = new_date[2]
        hour = hour_array[0]
        path_folder = '../GIR/' + year + '/' + month + '/' + day + '/' + hour
        for file in os.listdir(path_folder):
            if file.endswith(".mp3"):
                if file == row[7]:
                    shutil.copy(path_folder + '/' + file, 'new_path_folder/')
                    print("File Found", file)
                             
csvFile.close()

output wherever you run, Windows or Linux

DataTable to Pivot DataTable

\o/

So far so good, right!