Minor changes in your code:
def extractData():
filename = ("data.txt")
infile = open(filename,'r')
for x in infile.readlines():
x = x.strip()
if x.startswith(">"):
header = x
else:
sequence = x
if header.startswith(">b22"):
print(header, sequence)
header = ''
infile.close()
extractData()
Btw, you can use debugger to identify what is wrong with the flow of program. If you are new to Python then I would recommend using Eclipse with Pydev plugin for interactive debugging. Link for Tutorial on Pydev in Eclipse
Having said that, issue appears because if header.startswith(">b22")
is being evaluated for each line parsed from file. When you move it inside else
block it will only be evaluated after you are done parsing sequence (and it does not evaluate for header
lines, obviously).