python - Compare if two items from os.listdir are similar? -
ignore question. different actual question needed ask. people answered question, i'm sorry. in future, though.
read new thread here: opening files found os.listdir() , comparing lines inside?
basically, i'm running os.listdir() listing of files, , trying compare if 2 different files have similar names. how go this?
basically, code this:
config_dir = "/etc/netctl/" profiles = os.listdir(config_dir) in profiles: if os.path.isfile(config_dir + i): if in i: print "true" else: pass
i'm not sure use check similarities in names, though. however, know "if in i" checking same word... don't know how go saving last one...
i tried:
i2 = "" profiles = os.listdir(config_dir) in profiles: if os.path.isfile(config_dir + i): if i2 == "": i2 = print i2 elif i2 == i: continue if i2 in i: print "true" else: pass
i think might overthinking this, though. output of os.listdir:
['hooks', 'interfaces', 'examples', 'ddwrt', 'momandkids_wifiz', 'backups', 'momandkids']
the files ddwrt momandkids_wifiz , momandkids. basically, want detect names "momandkids" , "momandkids_wifiz" similar, , return true.
this should it:
from difflib import sequencematcher glob import glob os import path config_dir = '/etc/netctl' min_ratio = 0.90 # 90% profiles = dict((i, {'full_path': v, 'matches': [], 'file_name': path.splitext(path.split(v)[-1])[0]}) (i, v) in enumerate(glob(config_dir + '/*.*'))) k, v in profiles.items(): sm = sequencematcher(a=v['file_name'], b='') k, v in profiles.items(): if k == k or k in v['matches']: continue sm.set_seq2(v['file_name']) if sm.ratio() > min_ratio: v['matches'].append(k) v['matches'].append(k) # display output k, v in profiles.items(): print k, v
Comments
Post a Comment