code: def extract_ends(rec, sites, flank, fw, maxfragsize=800):
      
      nsites = len(sites)
      size = len(rec)
      for i, s in enumerate(sites):
          newid = "{0}:{1}".format(rec.name, s)
          recs = []
  
          if i == 0 or s - sites[i - 1] <= maxfragsize:
              newidL = newid + "L"
              left = max(s - flank, 0)
              right = s
              frag = rec.seq[left:right].strip("Nn")
              recL = SeqRecord(frag, id=newidL, description="")
              if i == 0 and s > maxfragsize:  # Contig L-end
                  pass
              else:
                  recs.append(recL)
  
          if i == nsites - 1 or sites[i + 1] - s <= maxfragsize:
              newidR = newid + "R"
              left = s
              right = min(s + flank, size)
              frag = rec.seq[left:right].strip("Nn")
              recR = SeqRecord(frag, id=newidR, description="")
              if i == nsites - 1 and size - s > maxfragsize:  # Contig R-end
                  pass
              else:
                  recs.append(recR)
  
          SeqIO.write(recs, fw, "fasta")
docs: Extraction of ends of fragments above certain size.