manipulating files within 'for'

B

Ben Keshet

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

Arnaud Delobelle

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

You should post the full error if you want some help which is more
than speculation
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?

I guess you are on Windows, but how do you run your script? Do you do
it from the command line or do you use some IDE?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top