473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 2016
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: von | last post by:
I am writing data from a Javascript to a text file using a Perl script and it all works pretty well - except ... This: "Here is my data" becomes: "Here%20is%20my%20data" when it gets to...
2
by: Kris Krieger | last post by:
Hello, Group, I hope it's OK for me to ask this question here, since I'm a "dabbler", not a "programmer". What I'd like to do, if it's possible, is find a reference or example/template that...
12
by: uno | last post by:
Hello, I am a beginner with the programming and I have some question. I am reading manuals from Internet and some book from the library. In all those documents the C language operate wint plain...
14
by: WUV999U | last post by:
Hi I am fairly familiar in C but not much. I want to know how I can write a html parser in C that only parses for the image file in the html file and display or print all the images found in...
1
by: Wanjun Yu | last post by:
I am a beginner, my simple question is what tool to use to write ASP pages? Thanks. Wanjun
3
by: Testguy | last post by:
Hi, I have been reading various messages in this group, and could not find an answer regarding what I am trying to do. At least, I could not find an easy answer. I am trying to figure out how...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
8
by: mohammaditraders | last post by:
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <string.h> class Matrix { private : int numRows, numCols ; int elements ;
10
by: andrew.smith.cpp | last post by:
Hello :-) how can i play a ".wav" file in C++.means with the help of the C++ code i just want to play a "a.wav" file more then 10 times. i just want to play background music. Can i do it in C++ ?...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.