0

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?

markm0311
  • 15
  • 5

1 Answers1

1

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])

markm0311
  • 15
  • 5
Joshua
  • 4,674
  • 1
  • 6
  • 30
  • Thank you. I played around with your original comment for awhile and couldn't get it to work. I will give this a go in just awhile and let you know what I come up with. – markm0311 Apr 09 '20 at 16:22
  • Infinity TM, the code works very well! Thanks again! I made a slight tweak to the final line to leave out the file extension: common_files.append(path.splitext(i)[0]) – markm0311 Apr 09 '20 at 23:57