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

Peter Otten __peter__ at web.de
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
    etc

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
demo.py  words.txt
$ cat words.txt
the
quick
brown
fox
$ cat demo.py
def convert_to_upper(infile, outfile):
    with open(infile) as instream:
        with open(outfile, "w") as outstream:
            outstream.write(instream.read().upper())

def convert_to_interspersed(infile, outfile):
    with open(infile) as instream:
        with open(outfile, "w") as outstream:
            outstream.write("-".join(instream.read()))

with open("words.txt") as words:
    for word in words:
        word = word.strip()
        filename = word
        with open(filename, "w") as f:
            f.write(word)
        convert_to_upper(filename, filename + ".upper")
        convert_to_interspersed(
            filename + ".upper",
            filename + ".interspersed"
        )
$ python demo.py
$ ls
brown               fox               quick.interspersed  the.upper
brown.interspersed  fox.interspersed  quick.upper         words.txt
brown.upper         fox.upper         the
demo.py             quick             the.interspersed
$ cat quick.interspersed 
Q-U-I-C-K$ 





More information about the Python-list mailing list