Good, I currently have the following code:
n = 0
with open('/home/user/test.filter') as f:
lines = f.readlines()
for l in lines:
if lines[n].startswith('-A vlan_8'):
if "-o bond1.8" in lines[n]:
f = open('vlan8.filter_in', 'a')
f.write(l)
else:
f = open('vlan8.filter_out', 'a')
f.write(l)
if lines[n].startswith('-A vlan_10'):
if "-o bond1.10" in lines[n]:
f = open('vlan10.filter_in', 'a')
f.write(l)
else:
f = open('vlan10.filter_out', 'a')
f.write(l)
if lines[n].startswith('-A vlan_15'):
if "-o bond1.15" in lines[n]:
f = open('vlan15.filter_in', 'a')
f.write(l)
else:
f = open('vlan15.filter_out', 'a')
f.write(l)
# [...]
n = n + 1
I thought about optimizing it with some accumulator or list to not make the script so extensive. Any suggestions?
== True- it is redundant; if the expression on the left side of==is equal toTrue, then, well, it's true. Second, as you have very similar code repeated multiple times, consider making it a function. Third, open your output files once, at the beginning, rather than opening them every time you need to write to them - you can keep a list, or better, a dict of them.ifclauses immediately under theforloop (except the first of course) could be turned intoelifs.if lines[n].startswithis incorrect, it should beif lines.startswith, but I didn't edit them to avoid changing the semantics of the question.