#!/usr/bin/python3 # ==================================================================== # match files in one directory with files in another directory # ==================================================================== import os dir_old = 'd:/abc' dir_new = 'd:/xyz' # -------------------------------------------------------------------- # ---- get a list of the filenames in a directory # -------------------------------------------------------------------- def get_list_of_files(dir): # --- get a list of entries in the directory entries = os.listdir(dir) # --- collect all of the filenames in the directory (skip links) files = [] for f in entries: ff = dir + '/' + f # path + filename if os.path.isfile(ff): if not os.path.islink(ff): files.append(f) files.sort() return files # -------------------------------------------------------------------- # ---- difference between two lists (list1 - list2) # -------------------------------------------------------------------- def difference_in_lists(list1,list2): diff = [] for element in list1: if element not in list2: diff.append(element) return diff # -------------------------------------------------------------------- # ---- main # -------------------------------------------------------------------- old_files = get_list_of_files(dir_old) new_files = get_list_of_files(dir_new) print() print(f'{len(old_files)} files found in old dir') print(f'{len(new_files)} files found in new dir') # ---- files in old dir but not in new dir print() dif = difference_in_lists(old_files,new_files) print('---- files in old dir but not in new dir') print(f'file differences is {len(dif)}') for i,f in enumerate(dif,1): print(f'[{i:03}] {f}') # ---- files in new dir but not in old dir print() dif = difference_in_lists(new_files,old_files) print('---- files in new dir but not in old dir') print(f'file differences is {len(dif)}') for i,f in enumerate(dif,1): print(f'[{i:03}] {f}') print()