472,144 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,144 software developers and data experts.

Write a file - beginner's question

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.
Jul 3 '08 #1
3 1946
Ben Keshet a écrit :
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 syntax is:

fileobj.write(something)

Now since you don't say exactly what you tried *and what you got*, we
can't help much more here.
the variable I want to write is
a line from a file I am reading:

"...
f = open('receptor.mol2', 'r')
line = f.readline()[:-1]
If you want to strip the newline characters, you'd better use
line.strip() or line.lstrip()
while '@<TRIPOS>ATOM' not in line:
line = f.readline()[:-1]
#print line

Also, there are simpler ways to iterate over a file:

f = open('somefile.txt')
for line in f:
if '@<TRIPOS>ATOM' in line:
break
print line.lstrip()

f.close()
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?
The simplest solution for a unix-like command-line program would be to
redirect the standard out to this file, ie:

# python myprog.py destfile.txt

But this may not fit your needs !-)
The other simplest solution is to open the destination file in write (or
append) mode and write to it:

source = open('somefile.txt')
dest = open('otherfile.txt', 'w')
for line in source:
if '@<TRIPOS>ATOM' in line:
break
# we don't strip here, since write doesn't append a newline
dest.write(line)

source.close()
dest.close()

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

source = open('/full/absolute/path/to/my/file.txt')

Note that the exact syntax for a path depends on your os (but the
os.path module can take care of most specificities). The above example
is for a posix system. On Windows, you'll probably have something like:

source = open('C:/full/absolute/path/to/my/file.txt')

HTH
Jul 3 '08 #2
I have a closely related note. How do I write to a relative path with
python? Example,
source = open('/../../directory/subdirectory/file.extension')

What am I missing?
Jul 3 '08 #3
Callie Bertsche wrote:
I have a closely related note. How do I write to a relative path with
python? Example,
source = open('/../../directory/subdirectory/file.extension')

What am I missing?
--
http://mail.python.org/mailman/listinfo/python-list
Leave off the beginning slash.

-Matt
Jul 3 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by von | last post: by
12 posts views Thread by uno | last post: by
14 posts views Thread by WUV999U | last post: by
1 post views Thread by Wanjun Yu | last post: by
24 posts views Thread by Bill | last post: by
8 posts views Thread by mohammaditraders | last post: by
10 posts views Thread by andrew.smith.cpp | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.