Trying to run a program within a python script on multiple output files creating by the same script

Peter Otten __peter__ at
Mon Apr 24 15:51:38 EDT 2017

Unaiza Batool wrote:

> i'm confused here as the script gives an error saying simple_to_fasta and
> fasta_to_mafft are not defined. 

You have to write this functions yourself (I gave one example) and make sure 
that they are defined before the 'for barcode' loop.

> How do I combine the part of infile,
> outfile with the conversion. You said it should go in the for barcode
> loop? Or just it just go after infname has it's correct value or just
> before each command when the new output file is needed?

A sketch of the final script (pseudo code, not runnable Python):

for barcode in barcodes
    write simple file
    convert simple file to fasta
    convert fasta file to mafft

To illustrate the idea here's a runnable example that reads words from a 
file into individual files, then converts them to upper case, then 
intersperses them with "-":

$ ls  words.txt
$ cat words.txt
$ cat
def convert_to_upper(infile, outfile):
    with open(infile) as instream:
        with open(outfile, "w") as outstream:

def convert_to_interspersed(infile, outfile):
    with open(infile) as instream:
        with open(outfile, "w") as outstream:

with open("words.txt") as words:
    for word in words:
        word = word.strip()
        filename = word
        with open(filename, "w") as f:
        convert_to_upper(filename, filename + ".upper")
            filename + ".upper",
            filename + ".interspersed"
$ python
$ ls
brown               fox               quick.interspersed  the.upper
brown.interspersed  fox.interspersed  quick.upper         words.txt
brown.upper         fox.upper         the             quick             the.interspersed
$ cat quick.interspersed 

More information about the Python-list mailing list