Python Forum
Compare filename with folder name and copy matching files into a particular folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare filename with folder name and copy matching files into a particular folder
#1
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\\ShantanuGupta\\Desktop\\OzSc1\\InputFolder"
# Search for folder in Location 1
destinationdir1 = r"C:\Users\ShantanuGupta\Desktop\OzSc1\OutputFolder1"
# Search for folder in Location 2
destinationdir2 = "C:\\Users\\ShantanuGupta\\Desktop\\OzSc1\\OutputFolder2"
# Put files in folder "Folder Not Found"
destinationdir3 = "C:\\Users\\ShantanuGupta\\Desktop\\OzSc1\\OutputFolder3\\FoldersThatCouldNotBeFound"

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
    if regex.match(files):
        print(files)

    found = False

    # Search for a folder in Location 1
    for folderName in os.listdir(destinationdir1):
        if regex.match(folderName):
            print(folderName)
            # Copy the files from the input folder to output folder
            shutil.copy(sourcedir+'/'+files, destinationdir1+'//'+folderName)
            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):
        if regex.match(folderName):
            #print(folderName)
            # Copy the files from the input folder to output folder
            shutil.copy(sourcedir+'/'+files, destinationdir1+'/'+folderName+'/'+files)
            found =  True
            break

    if not found:
        print('folder not found in Location2')
        count = 2

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

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...

Is there any better way to write this code?

Attached python code and folder structure here

Attached Files

Thumbnail(s)
       

.py   test.py (Size: 1.82 KB / Downloads: 227)
Reply


Messages In This Thread
Compare filename with folder name and copy matching files into a particular folder - by shantanu97 - Dec-18-2021, 11:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 299 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Deleting Windows temp folder Raysz 7 494 Apr-02-2024, 12:36 PM
Last Post: Raysz
  Help with creating folder and "virtual environment" AudunNilsen 1 260 Mar-21-2024, 04:41 AM
Last Post: deanhystad
Question How to add Python folder in Windows Registry ? Touktouk 1 295 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  Copy Paste excel files based on the first letters of the file name Viento 2 470 Feb-07-2024, 12:24 PM
Last Post: Viento
  Create dual folder on different path/drive based on the date agmoraojr 2 467 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 590 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  problem in import module from other folder akbarza 5 1,492 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  Reading a file name fron a folder on my desktop Fiona 4 937 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Rename files in a folder named using windows explorer hitoxman 3 772 Aug-02-2023, 04:08 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020