portable /dev/null

B

Brendan Miller

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
 
G

Giampaolo Rodola'

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
 
C

Christian Heimes

Brendan said:
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.

import os
null = open(os.devnull, "wb")

:)

Christian
 
C

Christian Heimes

Brendan said:
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.

import os
null = open(os.devnull, "wb")

:)

Christian
 
M

Martin v. Löwis

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?

The subprocess module should work fine. Pass a pipe to the subprocess,
and read and discard anything from the pipe (e.g. in a separate thread).
If the non-Python code is inprocess, you are out of luck. Some systems
just don't support a null device, so Python can't possibly emulate it.

Regards,
Martin
 
G

Gabriel Genellina

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?

py> import os
py> print os.devnull
nul

(this is on Windows)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top