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?

1
  • check my answer now Commented Apr 9, 2020 at 6:04

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

This does not seem to work if one or more of the directories is/are empty. The following seems to work to resolve this:

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 = []
    if len(files1)!=0:
        for i in files1:
            if len(files2)!=0:
                for j in files2:
                    if (path.splitext(i)[0] == path.splitext(j)[0]):
                        common_files.append(i)
                common_files = list(set(common_files)) # will reorder elements
            else:
                common_files = files1
    else:
        common_files = files2
        
    return common_files

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.