'Rename multiple files based on tuple used to sort

I am trying to sort/rename files that I automatically download from outlook. Each file comes from an athlete from one of three sports. Right now I have this loop able to sort the file into the correct folder based on the tuple but I was wondering if I can also make it rename the file at the same time based on the name in the tuple that identified where it should be sorted to.

source = r'C:\path\source\\'
dest1 = r'C:\path\dest1\\'
dest2 = r"C:\path\dest2\\"
dest3 = r"C:\path\dest3\\"

df_full_FB_roster = pd.read_csv('Full_FB_Roster.csv')
fullfb = df_full_FB_roster['name'].unique()
set(fullfb)

df_full_tennis_roster = pd.read_csv('Full_tennis_Roster.csv')
fulltennis = df_full_tennis_roster['name'].unique()
set(fulltennis)

df_full_bowling_roster = pd.read_csv('Full_bowling_Roster.csv')
fullfb = df_full_bowling_roster['name'].unique()
set(fullbowling)

files = os.listdir(source)


for f in files:
    if (f.startswith(tuple(fullfb))):
        shutil.move(source + f, dest1 + f)
    elif (f.startswith(tuple(fulltennis))):
        shutil.move(source + f, dest2 + f)
    elif (f.startswith(tuple(fullbowling))):
        shutil.move(source + f, dest3 + f)


Solution 1:[1]

Here is an example of how to do what your question asks, with a few preliminary comments:

  • You asked: "I was wondering if I can also make it rename the file at the same time based on the name in the tuple that identified where it should be sorted to". I would observe that the name in the tuple that identified where it should be sorted to is already part of the file name, so renaming the file based on that name may or may not be necessary (see the sample output below).
  • In the sample code below, I have hardcoded sample inputs that would be read from files, and I have commented out the move statement and simply printed it for testing purposes.

Sample code:

import pandas as pd
source = r'C:\path\source\\'
dest1 = r'C:\path\dest1\\'
dest2 = r"C:\path\dest2\\"
dest3 = r"C:\path\dest3\\"


df_full_FB_roster = pd.DataFrame({'name': ['John Doe', 'Jane Doe']})
fullfb = df_full_FB_roster['name'].unique()

df_full_tennis_roster = pd.DataFrame({'name': ['James Doe', 'Janice Doe']})
fulltennis = df_full_tennis_roster['name'].unique()

df_full_bowling_roster = pd.DataFrame({'name': ['Jack Doe', 'Janine Doe']})
fullbowling = df_full_bowling_roster['name'].unique()

files = ['John Doe foo.csv', 'Jane Doe foo.csv', 'James Doe foo.csv', 'Janice Doe foo.csv', 'Jack Doe foo.csv', 'Janine Doe foo.csv']
for f in files:
    sports = [[dest1, fullfb], [dest2, fulltennis], [dest3, fullbowling]]
    for dest, full in sports:
        matches = [name for name in tuple(full) if f.startswith(name)]
        if matches:
            name = matches[0]
            print(f'shutil.move({source}' + f'{f}, {dest}' + f'{name}_{f}')
            #shutil.move(source + f, dest + f'{name}_{f}')
            break

Output:

shutil.move(C:\path\source\\John Doe foo.csv, C:\path\dest1\\John Doe_John Doe foo.csv
shutil.move(C:\path\source\\Jane Doe foo.csv, C:\path\dest1\\Jane Doe_Jane Doe foo.csv
shutil.move(C:\path\source\\James Doe foo.csv, C:\path\dest2\\James Doe_James Doe foo.csv
shutil.move(C:\path\source\\Janice Doe foo.csv, C:\path\dest2\\Janice Doe_Janice Doe foo.csv
shutil.move(C:\path\source\\Jack Doe foo.csv, C:\path\dest3\\Jack Doe_Jack Doe foo.csv
shutil.move(C:\path\source\\Janine Doe foo.csv, C:\path\dest3\\Janine Doe_Janine Doe foo.csv

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 constantstranger