import os os.chdir('E:\\f5_automation') r = open('E:\\f5_automation\\uat.list.cmd.txt') #print(r.read().replace('\n', '')) t = r.read().split('\n') for i in range(len(t)): if ('inherited' or 'device-group' or 'partition' or 'template' or 'traffic-group') in t[i]: t.pop(i) print(i,t[i])