'While' question

Ben Keshet keshet1 at umbc.edu
Sat Aug 23 00:49:10 CEST 2008

Wojtek Walczak wrote:
> On Fri, 22 Aug 2008 10:42:13 -0400, Ben Keshet wrote:
>> Thanks.  I tried to use 'for' instead of 'while' as both of you 
>> suggested.  It's running well as my previous version but breaks 
>> completely instead of just skipping the empty file.  I suspect the 
>> reason is that this part is inside another 'for' so it stops 
>> everything.  I just want to it to break only one 'for', that is go back 
>> to 5th line in the example code (directory C has one empty file):
>>            for line in f:
>                         ^^^
>>                line = line.rstrip()
>>                if "PRIMARY" not in line:
>>                    j += 1
>>                    if j == 20:
>>                        break
>>                else:
>>                    for line in f:
>                                 ^^^
> You're iterating through the same value in inner and outer loop.
> Don't do that. It's hard to predict the behavior of such a code.
> Regarding break statement, it breaks only the inner loop
> and returns to the outer loop/block.
> It would be great if you could reduce your code to a short piece
> that illustrates your problem and that we could all run.
I ended up using another method as someone suggested to me.  I am still 
not sure why the previous version got stuck on empty files, while this 
one doesn't:

receptors = ['A' 'B']
for x in receptors:
    # open out_file for appending for each 'x' in receptors, close at 
same level
    out_file = 
open('c:/Linux/Dock_method_validation/%s/validation/pockets.out' %(x),'a')
    for i in range(10):
        for r in (7, 9, 11, 13, 15, 17):
            f = 
%(x,i,r), 'r')
            # assume 'PRIMARY' should be found first
            # set flag for string 'PRIMARY'
            primary = False
            # iterate on file object, empty files will be skipped
            for line in f:
                if 'PRIMARY' in line:
                    primary = True
                    # copy scores
                elif 'TRIPOS' not in line and primary:
                    out_file.write(' ')
                elif 'TRIPOS' in line and primary:

More information about the Python-list mailing list