开发者

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_empt开发者_开发百科y file is not empty.


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..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜