Something like this?
from pathlib import Path class Security: def __init__(self, match_value): ''' Note directory structure: put your selected file in threats directory. YourSourceDir/ data/ threats/ EmergingThreats.txt src/ thisProgram.py :param match_value: The value you want to select and write to output file ''' self.homepath = Path('.') self.rootpath = self.homepath / '..' self.datapath = self.rootpath / 'data' self.datapath.mkdir(exist_ok=True) self.threatpath = self.datapath / 'threats' self.threatpath.mkdir(exist_ok=True) self.emthreat_file = self.threatpath / 'EmergingThreats.txt' self.selections_out = self.threatpath / 'SelectedThreats.txt' self.select_output(match_value) def select_output(self, match_value): with self.emthreat_file.open('r') as f, self.selections_out.open('w') as f1: for line in f: line = line.strip() if line.startswith('#') or len(line) == 0: continue cidx = line.index('classtype') classtype = line[cidx:] cidx = classtype.index(';') classtype = classtype[:cidx] ctype = classtype.split(':') classtype = ctype[1] if classtype == match_value: f1.write('{}\n'.format(line)) def testit(): # change classtype you want selected here Security('misc-attack') if __name__ == '__main__': testit()You can modify this for your exact requirements