manipulating files within 'for'

Ben Keshet keshet1 at umbc.edu
Fri Sep 12 13:11:06 EDT 2008


Hi Pythoneers,

I have a question about a code I wrote with the help of someone. The 
code below copy a few lines from different files into one file. It works 
fine as it is given here and generates the new file 'pockets.out' 
correctly, but says:"....py returned exit code 0". However, if I add 
more values to 'receptor' (say, receptor = ['1AZM' '1ADS']) it gives an 
error: "Exception raised while running script".

Can anyone please advice me? Why is it giving an error on multiple x but 
runs well with one (I made sure that all files and folders exist, etc.). 
What does exit code 0 mean? what does "exception raised" mean?

Thanks a lot,
BK

CODE:

|receptors = ['1AZM']
for x in receptors:
    print x
    # 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 = open('%s/validation/ligand_ran_line_%s_%s.mol2' 
%(x,i,r), 'r')
            out_file.write('%s ' %i)
            out_file.write('%s ' %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
                    out_file.write(line.strip()) # write line to out_file
                    out_file.write(' ')               # add a space
                # write all line after 'PRIMARY' was found until 
'TRIPOS' is found
                elif 'TRIPOS' not in line and primary:
                    out_file.write(line.strip()) 
                    out_file.write(' ')                # add a space
                elif 'TRIPOS' in line and primary:
                    break                                # stop when 
'TRIPOS' is found
            print
            out_file.write('\n')   # move to a new line
            f.close()      # close file. for loop moves to next 'r' 
value, and then to next 'i'
    out_file.close()    # close out_file|



More information about the Python-list mailing list