-2

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)
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
AKtheAT
  • 29
  • 4
  • Sure you can, you just need to change the destination file name to whatever you want it to be. – Pranav Hosangadi May 11 '22 at 22:01
  • Does this answer your question? [How to rename a file using Python](https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python) Specifically, [this answer](https://stackoverflow.com/a/2491231/843953), although caveated by mvbentes's [comment there](https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python#comment79617912_2491231) – Pranav Hosangadi May 11 '22 at 22:01

1 Answers1

0

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
constantstranger
  • 9,176
  • 2
  • 5
  • 19
  • Thank you for reaching out to help me! I am going to try and implement this suggestion in the morning but I wanted to ask one question. Is it possible to setup the dataframe lines of code like you suggest above if each list of "names" that I have for each "team" is 100+ names and each team has its own list of names in its own csv file? Do I need to type out each individual name in the lines like "df_full_FB_roster = pd.DataFrame({'name': ['John Doe', 'Jane Doe']})" – AKtheAT May 12 '22 at 03:52
  • Thanks everyone for the help. I ended up solving my problem more quickly by changing the filename at the time of downloading the file based on the name of the email sender. I am still going to keep the above code for sorting them into the correct files. – AKtheAT May 12 '22 at 10:39
  • Glad to hear that you have a solution and that this answer was useful to you. Please feel free to mark the answer as "accepted". – constantstranger May 12 '22 at 11:42