portable /dev/null again

John Belmonte john at neggie.net
Wed Mar 24 22:39:31 CET 2004


Skip Montanaro wrote:
> In general you can't pass file descriptors between programs.

A library I'm using requires a file descriptor to output to, and I'd 
like to give it /dev/null.

> While I agree you can open /dev/null for reading, it's rarely used that way.

Agreed, I suggested it for completeness.

> If you want something that returns an honest-to-god file object, how about:
> 
>     import os
>     def nullfile(mode="wb"):
>         return file(os.path.exists("/dev/null") and
>                     "/dev/null" or
>                     "nul:", mode)

This code will break if Python ever supports a new OS that doesn't use 
one of those file names.

> Guido made an astute observation the other day which I can't find at the
> moment.  It went something like, "The biggest problem I have now is
> convincing people that just because something's not already in the standard
> library doesn't mean it belongs there."  I suspect this applies.  If a
> portable null file object is the cats pajamas for you, write something and
> throw it in your personal toolbox or post it to your personal Python page
> where others can find it.

I'm not interested in cats or pajamas, just the os library telling me 
what the null filename is for the OS I'm running on, just as it tells me 
what the path separator, etc. is.  I've already put an implementation in 
  my local library, but it may well break in the future as I've 
explained, and I feel other Python users facing this shouldn't have to 
do the same.

-John


-- 
http:// if  ile.org/




More information about the Python-list mailing list