[Tutor] Looking for Python equivalents of some C functions

Don Arnold Don Arnold" <darnold02@sprynet.com
Sat Feb 8 08:07:01 2003


----- Original Message -----
From: "Tony Cappellini" <tony@tcapp.com>
To: "Don Arnold" <darnold02@sprynet.com>
Sent: Saturday, February 08, 2003 2:14 AM
Subject: Re: [Tutor] Looking for Python equivalents of some C functions


>
> >thanks
>
>
> > >>> a = 'This string has %d %s.' % (5,'words')
> > >>> print a
> >This string has 5 words.
> >
> >This can be done using the print statement:
> >
> >myfile = open('temp')
> >print >> myfile, '%s %d %l' % ('stuff', 10, 28328738273L)
>
>  >>
> I've never seen this operator defined for redirection in any of my python
books
> It's always listed as the bitshift operator

I think it's a fairly recent additon (added in 2.1 or 2.2?).

>
> Here's what happens when I tried your example
> myfile=open("junk.txt","w")
>  >>> print >> myfile, '%s %d %l' % ('stuff', 10, 28328738273L)
> Traceback (most recent call last):
>    File "<interactive input>", line 1, in ?
> ValueError: incomplete format
>

Shame on me! Apologies to any and all who took this as working code. There
is no format specifier for a long int. Just format it as a string:


myfile = open('d:/python22/tutor/tony.txt','w')

print >> myfile, '%s %d %s' % ('stuff', 10, 28328738273L)
myfile.close()

myfile = open('d:/python22/tutor/tony.txt','r')
for line in myfile.readlines():
    print line

[--- begin program run --]

stuff 10 28328738273

[--- begin program run --]


This once again shows that no matter how simple your code looks, you should
run it through the interpreter before posting it. You'd think I would've
learned that by now! ; )

Sorry for the confusion,
Don