Home > Mobile >  How to differentiate files with same name with different extension from two different folders? Pytho
How to differentiate files with same name with different extension from two different folders? Pytho

Time:07-23

Problem Statement:- I have two folder with some data : (1) ImageFile , (2) TextFile

  1. Image_File Folder: It contains only image files with .jpg extensions.
Image_File = [A.jpg, B.jpg, C.jpg, D.jpg, E.jpg]
  1. TextFile Folder: It contains only text file with .txt extensions.
TextFile = [ A.txt, B.txt, C.txt]

Expected Output:-

The expected output should be as: Result = [ D.jpg, E.jpg]

CodePudding user response:

Assuming you have lists, you could use:

Image_File = ['A.jpg', 'B.jpg', 'C.jpg', 'D.jpg', 'E.jpg']
TextFile = ['A.txt', 'B.txt', 'C.txt']

basenames = {x.rsplit('.', maxsplit=1)[0] for x in TextFile}
# {'A', 'B', 'C'}

Result = [x for x in Image_File if x.rsplit('.', 1)[0] not in basenames]

output: ['D.jpg', 'E.jpg']

  • Related