By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,407 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,407 IT Pros & Developers. It's quick & easy.

Using print instead of file.write(str)

P: n/a
A.M
Hi,

I found print much more flexible that write method. Can I use print instead
of file.write method?

Thank you,

Alan
Jun 1 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
A.M a écrit :
Hi,
I found print much more flexible that write method. Can I use print instead
of file.write method?


f = open("/path/to/file")
print >> f, "this is my %s message" % "first"
f.close()

To print to stderr:

import sys
print >> sys.stderr, "oops"

FWIW, you and use string formating anywhere, not only in print statements:

s = "some %s and % formating" % ("nice", "cool")
print s

You can also use "dict formating":

names = {"other": "A.M.", "me" : "bruno"}
s = "hello %(other)s, my name is %(me)s" % names
Jun 1 '06 #2

P: n/a
Didn't know of the >> syntax: lovely to know about it Bruno - thank
you.

To the OP - I find the print statement useful for something like:
print 'this','is','a','test'
'this is a test' (with implicit newline and implicit spacing between parameters)

If you want more control (more flexibility, perhaps?) over the
formatting of the output: be it spacing between parameters or newline
control, use the methods Bruno describes below.

I'm not sure if you can suppress the spacing between elements (would
love to be corrected though); to stop the implicit newline use
something like
print 'testing', 'testing'
(but - with the leading comma, the newline is suppressed)

I personally find that print is convenient for sentences (or writing
'lines').

Thought it worth pointing this out in case, like some I know, you come
across a cropper with certain output streams.

All the best,

Jon.

Bruno Desthuilliers wrote: A.M a écrit :
Hi,
I found print much more flexible that write method. Can I use print instead
of file.write method?


f = open("/path/to/file")
print >> f, "this is my %s message" % "first"
f.close()

To print to stderr:

import sys
print >> sys.stderr, "oops"

FWIW, you and use string formating anywhere, not only in print statements:

s = "some %s and % formating" % ("nice", "cool")
print s

You can also use "dict formating":

names = {"other": "A.M.", "me" : "bruno"}
s = "hello %(other)s, my name is %(me)s" % names


Jun 1 '06 #3

P: n/a
I meant 'trailing': not leading.

mea culpa.

Jon.

Jon Clements wrote:
Didn't know of the >> syntax: lovely to know about it Bruno - thank
you.

To the OP - I find the print statement useful for something like:
print 'this','is','a','test'
'this is a test' (with implicit newline and implicit spacing between parameters)

If you want more control (more flexibility, perhaps?) over the
formatting of the output: be it spacing between parameters or newline
control, use the methods Bruno describes below.

I'm not sure if you can suppress the spacing between elements (would
love to be corrected though); to stop the implicit newline use
something like
print 'testing', 'testing'

(but - with the leading comma, the newline is suppressed)

I personally find that print is convenient for sentences (or writing
'lines').

Thought it worth pointing this out in case, like some I know, you come
across a cropper with certain output streams.

All the best,

Jon.

Bruno Desthuilliers wrote:
A.M a écrit :
Hi,
I found print much more flexible that write method. Can I use print instead
of file.write method?


f = open("/path/to/file")
print >> f, "this is my %s message" % "first"
f.close()

To print to stderr:

import sys
print >> sys.stderr, "oops"

FWIW, you and use string formating anywhere, not only in print statements:

s = "some %s and % formating" % ("nice", "cool")
print s

You can also use "dict formating":

names = {"other": "A.M.", "me" : "bruno"}
s = "hello %(other)s, my name is %(me)s" % names


Jun 1 '06 #4

P: n/a
A.M
Yes, it saved my time big time.

Thank you Bruno.

I use the print >>>file to generate HTML files. print is very flexible and
nice.

The dictionary formatting that Brunto said is awesome!

Thanks again,

Alan

"Jon Clements" <jo****@googlemail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
Didn't know of the >> syntax: lovely to know about it Bruno - thank
you.

To the OP - I find the print statement useful for something like:
print 'this','is','a','test'
'this is a test' (with implicit newline and implicit spacing between parameters)

If you want more control (more flexibility, perhaps?) over the
formatting of the output: be it spacing between parameters or newline
control, use the methods Bruno describes below.

I'm not sure if you can suppress the spacing between elements (would
love to be corrected though); to stop the implicit newline use
something like
print 'testing', 'testing'
(but - with the leading comma, the newline is suppressed)

I personally find that print is convenient for sentences (or writing
'lines').

Thought it worth pointing this out in case, like some I know, you come
across a cropper with certain output streams.

All the best,

Jon.

Bruno Desthuilliers wrote: A.M a écrit :
Hi,
I found print much more flexible that write method. Can I use print
instead
of file.write method?


f = open("/path/to/file")
print >> f, "this is my %s message" % "first"
f.close()

To print to stderr:

import sys
print >> sys.stderr, "oops"

FWIW, you and use string formating anywhere, not only in print statements:

s = "some %s and % formating" % ("nice", "cool")
print s

You can also use "dict formating":

names = {"other": "A.M.", "me" : "bruno"}
s = "hello %(other)s, my name is %(me)s" % names

Jun 1 '06 #5

P: n/a
A.M <al******@newsgroup.nospam> wrote:
I found print much more flexible that write method.


"more flexible"? More convenient, yes. More powerful, maybe. But I
don't see more flexible. Everything print can to stdout.write() can
do. The reverse isn't true. eg (this appears to be a FAQ on this
group, although I can't find it in the FAQ):

for x in range(10):
sys.stdout.write(str(x))

to print:

0123456789

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jun 2 '06 #6

P: n/a
Sion Arrowsmith a écrit :
A.M <al******@newsgroup.nospam> wrote:
I found print much more flexible that write method.

"more flexible"? More convenient, yes. More powerful, maybe. But I
don't see more flexible. Everything print can to stdout.write() can
do. The reverse isn't true. eg (this appears to be a FAQ on this
group, although I can't find it in the FAQ):

for x in range(10):
sys.stdout.write(str(x))

to print:

0123456789

The reverse isn't true ???

print "".join(str(x) for x in range(10))

(which BTW should be faster, since it's only one I/O, instead of ten
with your version.)

Now, given:

bird = "parrot"
beautiful = "dead"

How would you do the following with f.write() ?

print "this", bird, "is", beautiful

(without using string formating, of course...)
Jun 2 '06 #7

P: n/a
Bruno Desthuilliers <bd*****************@free.quelquepart.fr> wrote:
Sion Arrowsmith a écrit :
A.M <al******@newsgroup.nospam> wrote:
I found print much more flexible that write method.


"more flexible"? More convenient, yes. More powerful, maybe. But I
don't see more flexible. Everything print can to stdout.write() can
do. The reverse isn't true. eg (this appears to be a FAQ on this
group, although I can't find it in the FAQ):

for x in range(10):
sys.stdout.write(str(x))

to print:

0123456789


The reverse isn't true ???

print "".join(str(x) for x in range(10))


What he meant it that it is impossible to produce "0123456789" using 10
separate print statements, while it IS possible with 10 separate writes.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 3 '06 #8

P: n/a
Tim Roberts <ti**@probo.com> wrote:
Bruno Desthuilliers <bd*****************@free.quelquepart.fr> wrote:
Sion Arrowsmith a écrit :
A.M <al******@newsgroup.nospam> wrote:

I found print much more flexible that write method.

"more flexible"? More convenient, yes. More powerful, maybe. But I
don't see more flexible. Everything print can to stdout.write() can
do. The reverse isn't true. eg (this appears to be a FAQ on this
group, although I can't find it in the FAQ):

for x in range(10):
sys.stdout.write(str(x))

to print:

0123456789


The reverse isn't true ???

print "".join(str(x) for x in range(10))


What he meant it that it is impossible to produce "0123456789" using 10
separate print statements, while it IS possible with 10 separate writes.


it's not quite impossible, just cumbersome:
for x in range(10):

.... print x,
.... sys.stdout.softspace=0
....
0123456789>>>

Yes, you do need the softspace assignments -- but then, in the write
version you need the explicit str calls, so it's not as if in either
case you're using "just" the print or write-call.

The differences in terms of convenience are surely there (in different
circumstances they will favor one or the other of the two approaches),
but I don't see such differences in either flexibility or power (if one
ignores the issue of convenience, the same tasks can be performed with
either approach).
Alex
Jun 4 '06 #9

P: n/a
On 3/06/2006 9:47 AM, Bruno Desthuilliers wrote:

Now, given:

bird = "parrot"
beautiful = "dead"

How would you do the following with f.write() ?

print "this", bird, "is", beautiful

(without using string formating, of course...)


Like this:

f.write((' '.join(str(x) for x in ['this', bird, 'is', beautiful]) + '\n'))

.... or was that a rhetorical question?

Cheers,
John
Jun 4 '06 #10

P: n/a
Tim Roberts a écrit :
Bruno Desthuilliers <bd*****************@free.quelquepart.fr> wrote:

Sion Arrowsmith a écrit :
(snip)
"more flexible"? More convenient, yes. More powerful, maybe. But I
don't see more flexible. Everything print can to stdout.write() can
do. The reverse isn't true. eg (this appears to be a FAQ on this
group, although I can't find it in the FAQ):

for x in range(10):
sys.stdout.write(str(x))

to print:

0123456789


The reverse isn't true ???

print "".join(str(x) for x in range(10))


What he meant it that it is impossible to produce "0123456789" using 10
separate print statements, while it IS possible with 10 separate writes.


why on earth would someone try to use 10 consecutive I/O operations on
the same stream when it can be done with 1 ???
Jun 4 '06 #11

P: n/a
John Machin a écrit :
(snip)
... or was that a rhetorical question?


It was.
Jun 4 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.