1

I'm trying to run a series of commands for two input files, but I want to avoid doing the same thing if it has already been done. To make it more clear, these are my commands:

from subprocess import call
import os.path

pathway = raw_input("Please give the pathway of your Genome Folder ( .fa files): ")
genome= raw_input("Please type your genome name (e.g. chick_build2.1):" )
#Building a genome (.stidx for stampy)
cmd = "python /space/tierzucht/mgholami/stampy-1.0.13/stampy.py -G %s %s/*.fa" %(genome, pathway)
#Building a genome (.fa for BWA)
cmd = "cat %s/*.fa > %s.fa" %(pathway, genome)
#Building an index (for BWA)
cmd = "bwa index %s.fa " %(genome)
call(cmd, shell=True)

and what I want to do is to add an if loop before each command that if the file already exists do not run this command. I tried this but it doesn't work:

if os.path.isfile("%s.stidx") %genome == False:
  cmd = "python /space/tierzucht/mgholami/stampy-1.0.13/stampy.py -G %s %s/*.fa" %(genome, pathway)

what I'm trying to do is to check if the stidx format of the chosen file name already exists! And I want to do it with fa format and several others too.

1
  • Why aren't you using make for this? Commented Nov 2, 2011 at 13:41

1 Answer 1

1

Try:

if not os.path.isfile("%s.stidx"%genome):
  cmd = "python /space/tierzucht/mgholami/stampy-1.0.13/stampy.py -G %s %s/*.fa" %(genome, pathway)

In your original code, the paretheses were misplaced (the % was getting interpreted as the remainder operator, so the interpreter did not complain about a syntax error).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.