Regex to compare filename with foldername and then copy matching filenames

Viewed 248

I am trying to write a python script that will use a regex to compare the file name that is present in the input folder with the output folder and if it matches, we, will copy that file from that input folder to another output folder.

Remember, we are comparing the filename that is present in the input folder name with the output folder name.

For Example:

Input folder: The filename looks like this "A10620.csv"

Output folder 1: There is one folder with the name "A10620_RD19CAR2000" besides the other folder name.

In output folder 1, I need to search the folder with that filename(See first 6 characters only), If they match, then we copy the files.

I need to search for that filename in two folder locations(Output folder 1 & Outputfolder2), If the folder does not found in location 2. Then we dumps the files in the "Files not found" folder at location 3.

Please see attached picture to get an idea of how the folder structure looks.

Here is my python script.

import os
import re
import shutil

# Traverse each file
sourcedir = "C:\\Users\\Desktop\\OzSc1\\InputFolder"
# Search for folder in Location 1
destinationdir1 = "C:\\Users\\Desktop\\OzSc1\\OutputFolder1"
# Search for folder in Location 2
destinationdir2 = "C:\\Users\\Desktop\\OzSc1\\OutputFolder2"
# Put files in folder "Folder Not Found"
destinationdir3 = "C:\\Users\\Desktop\\OzSc1\\OutputFolder3"

regex = re.compile(r'^A\d{5}', re.IGNORECASE) #Checking only first six characters

count = 0
for files in os.listdir(sourcedir):  # looping over different files
    # print(files)
    if regex.match(files):
        print(files)

    # Search for a folder in Location 1
    for folderName in os.listdir(destinationdir1):
        # print(folderName)
        if regex.match(folderName):
            print(folderName)
            # Copy the files from the input folder to output folder
            shutil.copy(sourcedir+'/'+files, destinationdir1+'/'+files)
        else:
            print('foldernotfoundinLocation1')
            count = 1

        # Search for a folder in Location 2
        for folderName in os.listdir(destinationdir2):
            # print(folderName)
            if regex.match(folderName):
                print(folderName)
                # Copy the files from the input folder to output folder
                shutil.copy(sourcedir+'/'+files, destinationdir1+'/'+files)
            else:
                print('foldernotfoundinLocation2')
                count = 2

            # Folder Not Found
            if (count == 2):
                print('copyingfilesinfoldernotfound')
                # Copy the files from the input folder to folder not found
                shutil.copy(sourcedir+'/'+files, destinationdir3+'/'+files)

Problems:

  1. In the input folder there are multiple files, I am having a difficulty in make a logic how to get the filename for 1 file at a time and search in different folder location. Then go for second file name and search in different folders and so on...

  2. Is there any better way to write this code?

folder structure from inside

How the folder structure looks like

Attached python code and folder structure here

1 Answers

Here is a way to look for the file. If it is not in not in location 1 and not in location 2 it will be copied in location 3.

I added a found variable to remember if file was found or not (and removed the count variable). When a file is found I break the loop as I suppose there will be only one match. The file is copied only if it is not found in location 1 nor in location 2.

import os
import re
import shutil

# Traverse each file
sourcedir = "C:\\Users\\Desktop\\OzSc1\\InputFolder"
# Search for folder in Location 1
destinationdir1 = "C:\\Users\\Desktop\\OzSc1\\OutputFolder1"
# Search for folder in Location 2
destinationdir2 = "C:\\Users\\Desktop\\OzSc1\\OutputFolder2"
# Put files in folder "Folder Not Found"
destinationdir3 = "C:\\Users\\Desktop\\OzSc1\\OutputFolder3"

regex = re.compile(r'^A\d{5}', re.IGNORECASE) #Checking only first six characters

#count = 0
for files in os.listdir(sourcedir):  # looping over different files
    # print(files)
    if regex.match(files):
        print(files, 'found')
        root = regex.match(files).group()         ## keep same root file name
        regex2 = re.compile(root, re.IGNORECASE)  ## keep same root file name
        print(root, 'root')

        found = False
    
        # Search for a folder in Location 1
        for folderName in os.listdir(destinationdir1):
            print('fname=',destinationdir1+'\\'+folderName, os.path.isdir(destinationdir1+'\\'+folderName))
            if regex2.match(folderName) and os.path.isdir(destinationdir1+'\\'+folderName):
                print('folder',destinationdir1+'\\'+folderName)
                # Copy the files from the input folder to output folder
                print('copy '+sourcedir+'\\'+files, destinationdir1+'\\'+files)
                shutil.copy(sourcedir+'/'+files, destinationdir1+'/'+files)
                found = True
                break
    
        if not found:
            print('folder not found in Location1')
            #count = 1
    
        # Search for a folder in Location 2
        for folderName in os.listdir(destinationdir2):
            # print(folderName)
            #print('fname=',destinationdir2+'\\'+folderName, os.path.isdir(destinationdir2+'\\'+folderName))
            if regex2.match(folderName) and os.path.isdir(destinationdir2+'\\'+folderName):
                print('folder',destinationdir2+'\\'+folderName)
                # Copy the files from the input folder to output folder
                print('copy '+sourcedir+'\\'+files, destinationdir1+'\\'+files)
                shutil.copy(sourcedir+'/'+files, destinationdir1+'/'+files)
                found =  True
                break
    
        if not found:
            print('folder not found in Location2')
            #count = 2
    
        # Folder Not Found
        if not found:
            print('copying files to not found folder')
            # Copy the files from the input folder to folder not found
            print('copy '+sourcedir+'\\'+files, destinationdir3+'\\'+files)
            shutil.copy(sourcedir+'/'+files, destinationdir3+'/'+files)
            
        
Related