python - Opening files found from os.listdir() and comparing lines inside? -
alright, i'm writing program connect wireless networks. have of down (in fact, it's complete. i'm working on features.)
i'm writing gui frontend wireless network connection backend called netctl arch linux operating system. basically, people can manually create profiles , name whatever want (i.e., "asdfasdfasdf"), mine generate $networkssid_wifiz.
however, every file have 1 line in able determine if same network.
the line is:
essid='$networkssid'
so how go opening each file appears in os.listdir , checking if 2 files have same line (while not producing overhead, preferably.)?
all profiles saved in /etc/netctl whether generated program, or user.
sample files:
user created:
description='a simple wpa encrypted wireless connection' interface=wlp2s0 connection=wireless security=wpa ip=dhcp essid='momandkids' # prepend hexadecimal keys \" # if key starts ", write '""<key>"' # see also: section on special quoting rules in netctl.profile(5) key='########' # uncomment if ssid hidden #hidden=yes
created program:
description='a profile generated wifiz momandkids' interface=wlp2s0 connection=wireless security=wpa essid='momandkids' key='#######' ip=dhcp
sample os.listdir output:
['hooks', 'interfaces', 'examples', 'ddwrt', 'momandkids_wifiz', 'backups', 'momandkids']
this should work you:
from glob import glob os import path config_dir = '/etc/netctl' profiles = dict((i, {'full_path': v, 'essid': none, 'matches': []}) (i, v) in enumerate(glob(config_dir + '/*')) if path.isfile(v)) k, v in profiles.items(): open(v['full_path']) f: line in f: if line.startswith('essid'): v['essid'] = line.split('=',1)[1].strip() break # no need keep reading. k, v in profiles.items(): if k == k or k in v['matches'] or not v['essid']: continue if v['essid'] == v['essid']: v['matches'].append(k) v['matches'].append(k) k, v in profiles.items(): print k, v
Comments
Post a Comment