How can I confirm and remove the original files after sorting and copying them into several folders?

Viewed 76

I'm a newbie and I'm trying to make office work a little less tedious. I currently have a little program that sorts and copies .pdf files from a folder into several folders, based on who these files need to be sent to later.

It works great. There's just the issue that I keep double-checking if it did its job. So then I added a bit where it counts the copied files to make checking easier.

Now I've been trying to figure out if I could make the program compare the list of files in the original folder with a list of files from all the other destination folders and then delete the originals if the files are indeed copied.

I've also resorted to having the program print the resulting file paths, but it's ugly and still requires me to manually compare.

Here's my code:

import os
import shutil
import pathlib
import pprint





dir = ('[path to original folder]')

files = os.listdir(dir)

user_data = [
    ('Karl H. Preusse', [Path to Karl]),
    ('Rom', [Path to Rom]),
    ('Hochschule', [Path to Hochschule]),
    ('Kiefer', [Path to Kiefer),
    ('Penny', [Path to Penny),
    ('Steigenberger', [Path to Steigenberger]),
    ('Penzkofer', [Path to Penzkofer]),
    ('Stoffel', [Path to Stoffel]),
    ('Cavertitzer', [Path to Cavertitzer])
]

for pattern, dest_dir in user_data:
    matching_files = [f for f in files if pattern in f]
    for filename in matching_files:
        full_filename = os.path.join(dir, filename)
        if os.path.isfile(full_filename):
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
            shutil.copy(full_filename, dest_dir)
            pprint.pprint(shutil.copy(full_filename, dest_dir))


stetje_datotek = sum(len(files) for _, _, files in os.walk([Path to directory that holds the copy folders])) #defines files to count
print('Stevilo datotek v mapi Posiljanje je: {}' .format(stetje_datotek)) #Prints out how many files are in the target folders.

Below are my attempts at getting things automated.

#I commented this function out as I couldn't figure out how to get the data out of it.

#def sub_files(folder):
#    relpath = os.path.relpath
 #   join = os.path.join
  #  for path, _, files in os.walk([Path to directory that holds the copy folders]):
   #     relative = relpath(path, [Path to directory that holds the copy folders])
    #    for file in files:
     #       yield join(relative, file)

#print(sub_files)

Here I thought to use inputs to individually check each folder:

#print(os.listdir([Path to directory that holds the copy folders]))
#if input() == 'Penzkofer':
    #pprint.pprint(os.listdir([Path to Penzkofer folder]))

And here I tried to compare lists, but I get a TypeError: unhashable type: 'list' error

prvotne_datoteke = set(os.listdir(dir))
kopirane_datoteke = set(os.walk([Path to directory that holds the copy folders])
set(prvotne_datoteke).intersection(kopirane_datoteke)

Any help is appreciated. Thank you.

1 Answers

One approach is to print the names of each copied file recipient and the number of recipients, then delete the original file if all intended recipients are included.

to_be_copied = set() # holds original paths of all files being copied 

for pattern, dest_dir in user_data:
    matching_files = [f for f in files if pattern in f]
    for filename in matching_files:
        full_filename = os.path.join(dir, filename)
        to_be_copied.add(filename) # adds filepaths 
        if os.path.isfile(full_filename):
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
            shutil.copy(full_filename, dest_dir)
            pprint.pprint(shutil.copy(full_filename, dest_dir))

# Iterates through copied files
for original_file in to_be_copied:
    count = 0
    recipients = []
    
    # Iterates through potential recipients
    for pattern, dest_dir in user_data:
        complete_name = os.path.join(dest_dir, original_file)
        if os.path.isfile(complete_name):
            count += 1
            recipients.append(pattern)
    
    print(original_file + ' sent to ' + str(count) + ' people:')
    print(recipients)
    
    # Quick manual check, could be changed to checking if count/recipients is correct
    print('Delete original file? (Y or N): ')
    delete = input()
    if (delete == 'Y'):
        os.remove(os.path.join(dir, original_file))
        

        
Related