473,719 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>A TOM" (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 2038
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(s omething)

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>A TOM" (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
6255
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 the text file.
2
1620
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 will show me how to create a "quote gallery" that uses a mouseover/mouseout to show/hide text in a layer - the idea is that, when the visitor mouses over a quote title, the appropriate text is called from a separate file and shows in a reserved and...
12
4626
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 text files, and I want to know how operate with formated files (msexcel, msaccess, mysql, etc..). If it isn't possible with C, with C++ is possible? Thanks a beginner (P.D.- Sorry for my poor english)
14
3139
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 the html file. How to go about it?
1
1810
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
35005
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 to read and XML file and put the data in fields in a VB application, and also how to write the information to an XML file (either to a new file or modifying an existing file.
24
4439
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 help out a newbie? #include <stdio.h> #include <ctype.h> #include <string.h>
8
2337
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
7063
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++ ? if yes then how ? Thanks
0
8726
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9361
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9210
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9054
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7997
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4482
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4746
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.