473,804 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2611
On 2 Mag, 21:20, Brendan Miller <catph...@catph ive.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******@catph ive.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1794
by: Nicolas Fleury | last post by:
Hi everyone, Is there an object in some standard library that would be the portable equivalent of "file('/dev/null','w')" or do I have to create my own class and object to do it? If it doesn't exist, do you think it would be a good idea to have such an object in a standard library? Thx and Regards, Nicolas
1
2132
by: JKop | last post by:
Would you classify the following code as "Undefined Behaviour" or as "Non- portable"? signed main() { signed char chedder = 130; } Relevant information:
4
2476
by: aaronfude | last post by:
Hi, Perhaps this is slightly offtopic. Coming into the Windows world from Unix. Is there a reference on writing C++ libraries in a portable way. For example, right now I'm sticking "__declspec(dllexport)" in front of each of my classes. I guess I can still make it work on Unix bydefing
3
1535
by: SenderX | last post by:
I am porting a library from msvc++ to gcc and POSIX and was wondering if the following was even close to being portable/c-std: #define MEM_CAST unsigned char* #define MEM_TO_OBJ( m, o ) ( (void*)( ((MEM_CAST)m) + sizeof( o ) ) )
7
2077
by: Robert Bachmann | last post by:
Two years I wrote a simple cesar encryption program, it worked but it relied on ASCII. So today I tried to make an portable cesar encryption. Please tell me if the code below is really protable. Thanks in advance. #include <stdlib.h> #include <stdio.h> #include <string.h>
131
6257
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write portable C code - *not only* because, in a 'C sense', I
13
2080
by: bwaichu | last post by:
I have written the program below to just create and populate an html file. I am running into a problem when viewing the created file in vi. I am told by vi that the file does not have an end of line. Here's the code: int main(int argc, char **argv) { FILE *fd;
6
4895
by: Bart Van Loon | last post by:
Hi all, I'm looking for a portable (FreeBSD and Linux) way of getting typical ifconfig information into Python. Some research on the web brought me to Linux only solutions http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439094 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439093
20
1990
by: upperclass | last post by:
Hi, I'm trying to find a decent way of measuring the running time of a long-running program. Say, the running time ranges from several seconds to more than a day. The standard clock() function seems inadequate for this task. (I read somewhere in this group that clock_t would likely overflow after <1 hour).
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10089
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9171
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.