469,323 Members | 1,583 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

portable /dev/null

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
Jun 27 '08 #1
6 2436
On 2 Mag, 21:20, Brendan Miller <catph...@catphive.netwrote:
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
Jun 27 '08 #2
Brendan Miller schrieb:
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
Jun 27 '08 #3
Brendan Miller schrieb:
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

Jun 27 '08 #4
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
Jun 27 '08 #5
En Fri, 02 May 2008 16:20:04 -0300, Brendan Miller <ca******@catphive.net>
escribió:
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?
pyimport os
pyprint os.devnull
nul

(this is on Windows)

--
Gabriel Genellina

Jun 27 '08 #6
On Fri, 02 May 2008 21:41:36 +0200, Christian Heimes wrote:
Brendan Miller schrieb:
>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
Awesome. Thanks.

Brendan
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Nicolas Fleury | last post: by
1 post views Thread by JKop | last post: by
4 posts views Thread by aaronfude | last post: by
3 posts views Thread by SenderX | last post: by
7 posts views Thread by Robert Bachmann | last post: by
131 posts views Thread by pemo | last post: by
13 posts views Thread by bwaichu | last post: by
6 posts views Thread by Bart Van Loon | last post: by
20 posts views Thread by upperclass | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.