Python find similiar files in given folder -
i'm trying write function find similiar files name (song.mp3, song1.mp3, (1)song.mp3) in specified folder. have now:
def print_duplicates(source): files_list = [] new_list = [] dirpath, dirnames, filenames in os.walk(source): fname in filenames: if ('\w*' + fname + '\w*') in files_list: new_list.append(os.path.join(dirpath, fname)) else: files_list.append(fname) in new_list: print(a)
if filename wasn't before in files_list added, if added new_list path. way have list of 'duplicate' files. it's not working, new_list remains empty. correct mistakes? part of code wrong?
if want use regex in code, need use re
module.
so change line,
if ('\w*' + fname + '\w*') in files_list:
to,
if re.search(r'\w*' + fname + r'\w*', files_list):
which same as,
if fname in file_list:
because \w*
means 0 or more word characters. , think want use word boundaries.
if re.search(r'\b' + fname + r'\b', files_list):
Comments
Post a Comment