Can someone explain to me how to test the program that I am writing in my command line? For example, I have to create a program that takes two or three parameters (an optional first parameter) and I am trying to create error messages for those who will be using the program but when I put in my parameters I don't get any message.
For example..
My program should print an error message and quit if: the inputfile does not end with .genes the outputfile does not end with .fa or .fasta
I have written in wings...
def extractGenes(variable,input,output):
variable = False
if '-s' in sys.argv[1]:
variable = True
if not input.endswith(".genes"):
print("Incorrect file type.")
sys.exit(1)
if not output.endswith(".fa") or output.endswith(".fasta"):
print("Incorrect file type.")
sys.exit(1)
I think this should work? but when I go to the putty command line to test it by writing python extractGenes.py (myinputfile) (myoutputfile), nothing happens.
1 answer
I don't think your program is actually called.
Everything seems to be inside the extractGenes-method, which is never called:
def extractGenes(variable,input,output):
variable = False
if '-s' in sys.argv[1]:
variable = True
if not input.endswith(".genes"):
print("Incorrect file type.")
sys.exit(1)
if not output.endswith(".fa") or output.endswith(".fasta"):
print("Incorrect file type.")
sys.exit(1)
Did I format this correctly? First of all, the method gets a "variable" from somewhere, which it then overwrites anyway. I don't understand what the variable is for anyway. I also don't really understand what your method is supposed to do - you give it an "input" and an "output" which both look like filenames, but at the same time, you look at the methods arguments in sys.argv? In the end, your program never prints anything if all input-variables are correctly named, so it's no wonder that nothing happens.
Here's how I would do it:
import sys
def main():
""" Usage: python script.py input.genes output.fa """
if not sys.argv[1].endswith(".genes"):
sys.stderr.write("Incorrect file type in input.\n")
sys.exit(1)
if not sys.argv[2].endswith(".fa") or not sys.argv[2].endswith(".fasta"):
sys.stderr.write("Incorrect file type in output.\n")
sys.exit(1)
print "Everything works fine!"
main()
Notice how I call the main-method in the end and use stderr to write error-messages.
Log in to answer this question.
Hi Yasmine, welcome to BioStars! http://stackoverflow.com/ is a good site to ask basic programming questions, here we try to address more specific bioinformatics questions.
closed as off topic