vagueish questions on use of file/string objects

Steve Holden sholden at
Tue Jan 29 00:53:50 EST 2002

"Keith Keller" <kkeller at> wrote in message
news:5dc53a.kmo.ln at
> Okay, bear with me here.
> I've just started coding a little Python, and I ran into a bit of
> a snag.  I would like to generate a news posting from sys.stdin, but
> only after doing some manipulations on it, mostly with the headers.
> Being stdin, though, it's not really possible to manipulate well, so
> I've been putting the post text into a string.
> However, the nntplib module demands a file object for its post method.
> So, I have five options:
> ==Open a temp file, put the post there, then use it with post.
> This works, of course, but then I'm accessing the disk, which doesn't
> strike me as being too elegant, since I can do everything I need to
> do in memory, and don't need disk storage.
> ==Subclass NNTP, and create a new method that accepts a string instead
> of a file (or override the post method which can detect whether it's
> being passed a file object or string object and act accordingly).  I've
> done this, and it works (well, except for error checking the string,
> but there's not an immense amount of error checking in nntplib's method,
> anyway).  But...well, it seems odd.
> ==Create a fake file object that's not on disk, and pass it to the
> nntplib post method.  I looked all over the place for docs on how to do
> this, but couldn't find anything helpful.  Either I'm clueless, the
> docs are buried, or they don't exist.
> I suppose the vague question is: What would be the ''standard'' Python
> way to handle my dilemma?  Is there another way I'm overlooking?

All your approaches seem quite reasonable, but your penultimate paragraph
hits the nail precisely on the head. Pythonistas talk about "file-like
objects", the most convenient of which would, for your purposes, probably be
found in the StringIO module. Faster versions are found in cStringIO.

Hope this helps. Good luck.

Consulting, training, speaking:
Python Web Programming:

More information about the Python-list mailing list