python equivalent for fputc

John Machin sjmachin at lexicon.net
Wed Aug 30 21:32:28 EDT 2006


Putty wrote:
> I'm porting a program a friend wrote in C over to Python and I've run
> into a little hang-up.  The C program writes characters out to a file.
> I'm 99% sure that a conversion is going on here as well.  I know for a
> fact that it's taking a number and turning it into a character.

C is a low-level language -- a character *is* a number :-)

>
> So what kind of call can I make to do it in Python?

*Guessing* that you mean e.g. fputc(97, f) writes the character 'a' to
the file whose handle is f ...

In Python the more-or-less literal translation (ignoring the fputc
return value) would be f.write(chr(97))

Note:
|>>> ord('a')
97
|>>> chr(97)
'a'

HTH -- if not, show us (relevant parts of) the actual source that you
are porting.

Cheers,
John




More information about the Python-list mailing list