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
#3
In version 2, the get_folder_contents get all files in folder, plus files that are in subdirectories of folder. For obvious reasons this will not work if you want to find out what files are in C:\Folder-Oana\extracted but are not in C:\Folder-Oana\extracted\translated. All files in html_files_folder2 will also be in html_files_folder1 because folder 2 is a subdirectory of folder 1.

I don't know why version 1 wouldn't work. It worked fine for me.
import os

a = set(f.lower() for f in os.listdir(".") if f.lower().endswith(".py"))
b = set(f.lower() for f in os.listdir("./test") if f.lower().endswith(".py"))

print("A or B", *(a | b), sep="\n")
print("", "A and B", *(a & b), sep="\n")
print("", "A but not B", *(a - b), sep="\n")
print("", "B but not A", *(b - a), sep="\n")
Output:
A or B junk.py console.py junk2.py junk3 copy.py junk3.py pythonhighlighter.py interactiveconsole.py sqlite_demo.py monkeypatching.py A and B junk.py junk2.py junk3.py A but not B console.py pythonhighlighter.py interactiveconsole.py sqlite_demo.py monkeypatching.py B but not A junk3 copy.py
There may be a slight problem with Larz60+ code. Files with the extension ".HTML" will not be included in the list because "html" != "HTML".

Stop using "\" and start using "/" for the separator in file paths. Windows accepts "/" and it eliminates the confusion of "\" maybe being the start of an escape sequence.
Reply


Messages In This Thread
RE: Compare folder A and subfolder B and display files that are in folder A but not in su - by deanhystad - Jan-05-2024, 04:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete file with read-only permission, but write permission to parent folder cubei 6 22,270 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  Deleting Windows temp folder Raysz 7 701 Apr-02-2024, 12:36 PM
Last Post: Raysz
  Help with creating folder and "virtual environment" AudunNilsen 1 320 Mar-21-2024, 04:41 AM
Last Post: deanhystad
Question How to add Python folder in Windows Registry ? Touktouk 1 378 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  Create dual folder on different path/drive based on the date agmoraojr 2 575 Jan-21-2024, 10:02 AM
Last Post: snippsat
  problem in import module from other folder akbarza 5 1,756 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  Reading a file name fron a folder on my desktop Fiona 4 1,080 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Rename files in a folder named using windows explorer hitoxman 3 857 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,736 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 1,065 Jun-26-2023, 09:40 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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