I am trying to run a script (see below) to read in a fasta file and output a taxonomy file (printing only the sequence header without the '>' character) but I keep getting a syntax error which I have not been able to resolve. As a result the script creates the cleanseqs.tax file but the file is blank. Could anyone help?
Thank you!
>>> Fasta = open("testseqs.fasta", "r")
>>> Tax = open("cleanseqs.tax", "w")
>>> while 1:
... SequenceHeader= Fasta.readline()
... Sequence= Fasta.readline()
... if SequenceHeader == '':
... break
... Tax.write(SequenceHeader.replace('>', ''))
... Fasta.close()
File "<stdin>", line 7
Fasta.close()
^
SyntaxError: invalid syntax
>>> Tax.close()
A file object is a context manager, so you can use the with statement to automatically close the files:
with open("testseqs.fasta", "r") as fasta, open("cleanseqs.tax", "w") as tax:
while True:
...