Question: (Closed) New To Python And Putty: Help!
0
gravatar for Yasmine
5.4 years ago by
Yasmine0
Yasmine0 wrote:

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.

python • 3.8k views
ADD COMMENTlink modified 5.4 years ago by Philipp Bayer4.6k • written 5.4 years ago by Yasmine0
1

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.

ADD REPLYlink written 5.4 years ago by Michael Kuhn4.9k

closed as off topic

ADD REPLYlink written 5.4 years ago by Michael Dondrup43k
6
gravatar for Philipp Bayer
5.4 years ago by
Philipp Bayer4.6k
Australia/Perth/UWA
Philipp Bayer4.6k wrote:

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.

ADD COMMENTlink modified 5.4 years ago • written 5.4 years ago by Philipp Bayer4.6k
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 818 users visited in the last hour