import sys server = 'https://rest.ensembl.org' ext = "/sequence/region/human/1:1179215..1197847:1?expand_3prime=0;expand_5prime=6" r = requests.get(server+ext, headers={ "Content-Type" : "text/x-fasta"}) if not r.ok: r.raise_for_status() sys.exit() with open("genes.fa", "a") as out: print(r.text.split()[0], file = out) print(r.text.split()[1][0:13], file = out) print(r.text.split()[1])