473,385 Members | 2,005 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,385 software developers and data experts.

printing to files in python

Hi-

I want to print stuff like:
"d1: %s, probability: %0.2f" % (d1, prob)
to a file, but when I do:
str = "d1: %s, probability: %0.2f" % (d1, prob)
outfile = open("out.txt", "w")
outfile.write(str)


I get errors. What am I missing?

Jul 18 '05 #1
2 2151
Matthew Wilson wrote:
Hi-

I want to print stuff like:
"d1: %s, probability: %0.2f" % (d1, prob)
to a file, but when I do:
str = "d1: %s, probability: %0.2f" % (d1, prob)
outfile = open("out.txt", "w")
outfile.write(str)
I get errors. What am I missing?


Apart from using str as a variable name, which is bad style, there is
nothing wrong with the code you have posted.
It would help, if you would also post the values of d1 and prob and cut and
paste the actual traceback.
Using the insufficient information you provide, prob could be an
incompatible type, e. g. a string
"%f" % "123.4" Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: float argument required

or you are ot allowed to write into the specified file:
file("out.txt", "w") Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 13] Permission denied: 'out.txt'


Or something completely different...

Peter
Jul 18 '05 #2
"Matthew Wilson" <mw*****@sarcastic-horse.com> wrote in message news:<ma*********************************@python.o rg>...
str = "d1: %s, probability: %0.2f" % (d1, prob)
outfile = open("out.txt", "w")
outfile.write(str)


I get errors. What am I missing?


What are you missing? You are missing including your actual errors
in this question.

The only error I get when running your code (Python 2.3, Windows)
is "d1" and "prob" aren't defined. If I fix that then "out.txt" is
written as expected.

TTFN,
Tom.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fuzzyman | last post by:
I looked through the manual, I looked on the web and could find very little on this subject.... The closest I could find was : http://www.faqts.com/knowledge_base/view.phtml/aid/4549. Saying...
7
by: Kamilche | last post by:
I'm pretty desperate to get color syntax printing. I just tried out the evaluation version of 'Komodo', which several people in here suggested, and its print features are terrible. The text is...
1
by: uri bushey | last post by:
So I have a tkinter based little program in Python that has a canvas loaded with a WaveSurfer (http://www.speech.kth.se/wavesurfer/) widget. I am trying to print the contents of the WaveSurfer...
5
by: Donnal Walter | last post by:
We want to be able to print HTML or PDF files to a given printer from Python in a kind of batch mode (without opening a preview window or printer dialog for each file). The printer is on a network,...
0
by: Jim & Joanne Collins | last post by:
Craig, Thank you very much for your response. I've interspersed my questions in your reply. If you have answers to my question I would be extremely gratefull for your assistance! Thanks.
2
by: David Isaac | last post by:
I'd like to try personal financial management using Python. I just found PyCheckbook, but it does not support check printing. Is there a Python check printing application kicking around? Thanks,...
8
by: David Isaac | last post by:
"Alan Isaac" <aisaac0@verizon.net> wrote in message news:_A34e.2207$1r6.248@trnddc02... > I'd like to try personal financial management using Python. > I just found PyCheckbook, but it does not...
3
by: crashonyou | last post by:
hello again..i've been searching for quite some time now already looking for a solution to printing word documents with python..same thing for internet explorer..i was experimenting around with some...
1
by: crashonyou | last post by:
hello..would just like to ask a little help here regarding printing pdf files..i am currently making an automated system for printing files and i have encountered an interesting problem..i already...
4
by: Chris Seymour | last post by:
Hi All, I am working on a python script for my colleague that will walk a directory and search in the different files for a specific string. These pieces I am able to do. What my colleague wants...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
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...

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.