Tim
Tim

Reputation: 1

Python: Problem with if statement

I have problem with a if statement code below:

do_blast(x):
    test_empty = open('/home/rv/ncbi-blast-2.2.23+/db/job_ID/%s.blast' % (z), 'r')
        if test_empty.read() == '':
            test_empty.close()
            return 'FAIL_NO_RESULTS'
        else:
            do_something

def return_blast(job_ID):
     if job_ID == 'FAIL_NO_RESULTS':
        return '<p>Sorry no results :( boooo</p>'
    else:
        return open('/home/rv/ncbi-blast-2.2.23+/db/job_ID/job_ID_%s.fasta' % (job_ID), 'r').read()

For some reason the code tries to assign "job_ID" to the fasta file in return_blast even though it should have returned "sorry no results". I also understand the file names and extensions are different i have my reasons for doing this.

The code works perfectly when the test_empty file is not empty.

Upvotes: 0

Views: 469

Answers (2)

ars
ars

Reputation: 123568

Maybe some simple printf style debugging will help:

def return_blast(job_ID):
    print 'job_ID: ', job_ID
    # ... 

Then you can at least see what "job_ID" your function receives. This is crucial for trying to figure out why your if statement fails.

Upvotes: 0

Brendan Long
Brendan Long

Reputation: 54312

I'm not sure if this is the problem, but your code isn't indented correctly (and that matters in Python). I believe this is what you were wanting:

do_blast(x):
    test_empty = open('/home/rv/ncbi-blast-2.2.23+/db/job_ID/%s.blast' % (z), 'r')
    if test_empty.read() == '':
        test_empty.close()
        return 'FAIL_NO_RESULTS'
    else:
        do_something

def return_blast(job_ID):
    if job_ID == 'FAIL_NO_RESULTS':
        return '<p>Sorry no results :( boooo</p>'
    else:
        return open('/home/rv/ncbi-blast-2.2.23+/db/job_ID/job_ID_%s.fasta' % (job_ID), 'r').read()

I don't think your code would have even run though..

Upvotes: 1

Related Questions