Python Forum
Compare folder A and subfolder B and display files that are in folder A but not in su
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare folder A and subfolder B and display files that are in folder A but not in su
#1
Create a code that compares c:\Folder-Oana\extracted\ and c:\Folder-Oana\extracted\translated\ and shows me the files that are in the first folder, but not in the second. So, compare folder A and subfolder B and display files that are in folder A but not in subfolder B. So I have 1880 html files in folder A and only 50 html files in FOLDER B. So, cod must show me each files, the different between 1880 - 50.

This is my code (2 versions). I try with ChatGPT another, more ways, but didn't work. I believe the problem is that there are FOLDER + SUBFOLDER, not different folders with no other subfolders in it.

Version 1

import os

folder1 = r"C:\Folder-Oana\extracted"
folder2 = r"C:\Folder-Oana\extracted\translated"

# Obține lista de fișiere HTML din fiecare folder
html_files_folder1 = [f.lower() for f in os.listdir(folder1) if f.lower().endswith('.html')]
html_files_folder2 = [f.lower() for f in os.listdir(folder2) if f.lower().endswith('.html')]

# Găsește diferențele între cele două liste de fișiere
missing_files = list(set(html_files_folder1) - set(html_files_folder2))

# Afișează fișierele care lipsesc
if missing_files:
    print("Fișierele HTML care se găsesc în folderul 1, dar nu în folderul 2, sunt:")
    for filename in missing_files:
        print(filename)
else:
    print("Nu există fișiere HTML care se găsesc în folderul 1, dar nu în folderul 2.")
Version 2

import os

folder1 = r'C:\Folder-Oana\extracted\translated'
folder2 = r'C:\Folder-Oana\extracted'

# Funcție pentru a returna lista de fișiere HTML dintr-un folder
def get_html_files(folder):
    html_files = []
    for root, dirs, files in os.walk(folder):
        for file in files:
            if file.lower().endswith('.html'):
                html_files.append(file)
    return html_files

# Obține lista de fișiere HTML pentru fiecare folder
html_files_folder1 = get_html_files(folder1)
html_files_folder2 = get_html_files(folder2)

# Verifică fișierele care se găsesc în folderul 1, dar nu în folderul 2
missing_files = [file for file in html_files_folder1 if file not in html_files_folder2]

# Afișează fișierele și folderul corespunzător în care se găsesc
for file in missing_files:
    if file in html_files_folder1:
        print(f"Fișierul {file} se găsește în folderul {folder1}")
    if file in html_files_folder2:
        print(f"Fișierul {file} se găsește în folderul {folder2}")
Reply


Messages In This Thread
Compare folder A and subfolder B and display files that are in folder A but not in su - by Melcu54 - Jan-05-2024, 11:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Deleting Windows temp folder Raysz 7 580 Apr-02-2024, 12:36 PM
Last Post: Raysz
  Help with creating folder and "virtual environment" AudunNilsen 1 279 Mar-21-2024, 04:41 AM
Last Post: deanhystad
Question How to add Python folder in Windows Registry ? Touktouk 1 333 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  Create dual folder on different path/drive based on the date agmoraojr 2 507 Jan-21-2024, 10:02 AM
Last Post: snippsat
  problem in import module from other folder akbarza 5 1,586 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  Reading a file name fron a folder on my desktop Fiona 4 999 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Rename files in a folder named using windows explorer hitoxman 3 799 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,615 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  how do I open two instances of visual studio code with the same folder? SuchUmami 3 958 Jun-26-2023, 09:40 AM
Last Post: snippsat
  How i can use categories with folder? AnonymerAlias 2 637 Jun-06-2023, 03:44 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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