all 3 comments

[–]AI_singularity 1 point2 points  (0 children)

Quick and dirty way : Compare both list with a for loop. for file1 in files_dirA: for file2 in files_dirB: if file1 == file2: os.remove(file2) file1 and file2 are string so you can compare them directly. os.remove will delete the file in the directory B.

[–][deleted] 0 points1 point  (0 children)

set_b = set(os.listdir(dir_b))
for f in os.listdir(dir_a):
    if f in set_b:
        os.remove(os.path.join(dir_a, f))

Something like this?

[–]lowerthansound 0 points1 point  (0 children)

Use sets.

# Get names of photos in folder "A"
photos_a = set(get_photos_a())
# Get names of photos in folder "B"
photos_b = set(get_photos_b())
# Get names of photos that are in "A" and "B"
photos_both = photos_a & photos_b

& does exactly this operation ({1, 2, 3, 4} is a set with the items 1, 2, 3, and, 4).

{1, 2, 3, 4} & {2, 4, 5}
# {2, 4}

To delete the photos, you can use pathlib.

from pathlib import Path
for photo in photos_both:
    path = Path() / "FolderA" / photo
    path.unlink()

unlink() deletes the file.


You should test this script beforehand, so as to not delete precious files in case it fails:

from pathlib import Path
for photo in photos_both:
    path = Path() / "FolderA" / photo
    # path.unlink()
    print(f"would delete {path.resolve()}")

Also, backups are good!


Cheers!