Write a file - beginner's question

Ben Keshet keshet1 at umbc.edu
Thu Jul 3 11:02:20 EDT 2008


I have a probably simple beginner's question -

I have a script that I am currently able to print its output.  instead, 
i want to write it into a file - I tried different versions of write() 
but might have gotten the syntax wrong.  the variable I want to write is 
a line from a file I am reading:

"...
f = open('receptor.mol2', 'r')
line = f.readline()[:-1]
while '@<TRIPOS>ATOM' not in line:
    line = f.readline()[:-1]
    #print line
    print random_mol2
..."

e.g. I want to write to a file all the lines in 'receptor.mol2' up to 
the string "@<TRIPOS>ATOM" (one after the other).  Can anyone please advice?

On a related note - how do I read and write to a file that is not in the 
same directory? e.g how do I provide a pathway to an open command?

Please remember that I am a very beginner in Python and programming - 
Thank you.



More information about the Python-list mailing list