portable /dev/null

Giampaolo Rodola' gnewsg at gmail.com
Fri May 2 15:30:03 EDT 2008


On 2 Mag, 21:20, Brendan Miller <catph... at catphive.net> wrote:
> Hi,
>
> I have functions that take a file object and write to it. In some cases I
> just want to throw out what is written to that file object. I want
> something like open('/dev/null', 'w'), but portable.
>
> It needs to have an underlying file descriptor/file handle, as it will be
> passed to non python code.
>
> Is there a portable /dev/null somewhere in the standard library?
>
> Thanks,
> Brendan

I don't think so. You could define a dummy class emulating the file-
object API. Something like:

class NullDevice:
    "A writeable object that writes to nowhere -- like /dev/null."
    def write(self, s):
        pass

    closed = False
    def close(self):
        self.closed = True


--- Giampaolo
http://code.google.com/p/pyftpdlib



More information about the Python-list mailing list