473,382 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2566
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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...
1
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
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...
3
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...
7
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. ...
131
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...
13
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...
6
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 ...
20
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()...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.