Compare by NAME only, and not by NAME + EXTENSION using existing code; Python 3.x

864 views Asked by At

The python 3.x code (listed below) does a great job of comparing files from two different directories (Input_1 and Input_2) and finding the files that match (are the same between the two directories). Is there a way I can alter the existing code (below) to find files that are the same BY NAME ONLY between the two directories. (i.e. find matches by name only and not name + extension)?

comparison = filecmp.dircmp(Input_1, Input_2) #Specifying which directories to compare
common_files = ', '.join(comparison.common) #Finding the common files between the directories
TextFile.write("Common Files: " + common_files + '\n') # Writing the common files to a new text file
  1. Example:
    • Directory 1 contains: Tacoma.xlsx, Prius.txt, Landcruiser.txt
    • Directory 2 contains: Tacoma.doc, Avalon.xlsx, Rav4.doc

"TACOMA" are two different files (different extensions). Could I use basename or splitext somehow to compare files by name only and have it return "TACOMA" as a matching file?

1

There are 1 answers

2
Joshua Varghese On BEST ANSWER

To get the file name, try:

from os import path
fil='..\file.doc'
fil_name = path.splitext(fil)[0].split('\\')[-1]

This stores file in file_name. So to compare files, run:

from os import listdir , path
from os.path import isfile, join
def compare(dir1,dir2):
    files1 = [f for f in listdir(dir1) if isfile(join(dir1, f))]
    files2 = [f for f in listdir(dir2) if isfile(join(dir2, f))]
    common_files = []
    for i in files1:
        for j in files2:
            if(path.splitext(i)[0] == path.splitext(j)[0]): #this compares it name by name.
                common_files.append(i)
    return common_files

Now just call it:

common_files = compare(dir1,dir2)

As you know python is case-sensitive, if you want common files, no matter if they contain uppers or lowers, then instead of:

if(path.splitext(i)[0] == path.splitext(j)[0]):

use:

if(path.splitext(i)[0].lower() == path.splitext(j)[0].lower()):

You're code worked very well! Thank you again, Infinity TM! The final use of the code is as follows for anyone else to look at. (Note: that Input_3 and Input_4 are the directories)

def Compare():
    Input_3 = #Your directory here
    Input_4 = #Your directory here
    files1 = [f for f in listdir(Input_3) if isfile(join(Input_3, f))]
    files2 = [f for f in listdir(Input_4) if isfile(join(Input_4, f))]
    common_files = []
    for i in files1:
        for j in files2:
            if(path.splitext(i)[0].lower() == path.splitext(j)[0].lower()):
                common_files.append(path.splitext(i)[0])