/dev/null as a file-like object, or logging to nothing

  • Thread starter Torsten Bronger
  • Start date
T

Torsten Bronger

Hallöchen!

Is there a portable and simply way to direct file-like IO to simply
nothing? I try to implement some sort of NullLogging by saying

--8<---------------cut here---------------start------------->8---
import logging
if options.logging:
logging.basicConfig(level=logging.DEBUG, filename=options.logfile, filemode="w",
format='%(asctime)s %(name)s %(levelname)s %(message)s')
else:
# redirect logging to a memory buffer in order to simply ignore it.
import StringIO
logging.basicConfig(stream=StringIO.StringIO())
--8<---------------cut here---------------end--------------->8---

However, this consumes memory. Is there a better way?

Tschö,
Torsten.
 
M

Marc 'BlackJack' Rintsch

Is there a portable and simply way to direct file-like IO to simply
nothing? I try to implement some sort of NullLogging by saying

`os.devnull`?

Ciao,
Marc 'BlackJack' Rintsch
 
C

Carsten Haese

Hallöchen!

Is there a portable and simply way to direct file-like IO to simply
nothing? I try to implement some sort of NullLogging by saying

--8<---------------cut here---------------start------------->8---
import logging
if options.logging:
logging.basicConfig(level=logging.DEBUG, filename=options.logfile, filemode="w",
format='%(asctime)s %(name)s %(levelname)s %(message)s')
else:
# redirect logging to a memory buffer in order to simply ignore it.
import StringIO
logging.basicConfig(stream=StringIO.StringIO())
--8<---------------cut here---------------end--------------->8---

However, this consumes memory. Is there a better way?

This might work:

class LogSink(object):
def write(self, *args, **kwargs): pass
def flush(self, *args, **kwargs): pass

logging.basicConfig(stream=LogSink())

I haven't tested this thoroughly, so it's possible that there are more
methods that the stream is expected to implement.

HTH,
 
T

Torsten Bronger

Hallöchen!
`os.devnull`?

Yes, but I wasn't really sure how portable it is, in particular, on
Windows. So does

open(os.devnull, "w").write("Schallalla")

work on Windows like on Unix?

Tschö,
Torsten.
 
T

Torsten Bronger

Hallöchen!

Carsten said:
Is there a portable and simply way to direct file-like IO to
simply nothing? [...]

[...]

This might work:

class LogSink(object):
def write(self, *args, **kwargs): pass
def flush(self, *args, **kwargs): pass

logging.basicConfig(stream=LogSink())

I haven't tested this thoroughly, so it's possible that there are
more methods that the stream is expected to implement.

No, write and flush are enough. Thank, this works.

Tschö,
Torsten.
 
G

Gabriel Genellina

En Sat, 08 Sep 2007 14:42:14 -0300, Torsten Bronger
Yes, but I wasn't really sure how portable it is, in particular, on
Windows. So does

open(os.devnull, "w").write("Schallalla")

work on Windows like on Unix?

Yes. os.devnull=='nul' on Windows.
 
G

Gabriel Genellina

En Sat, 08 Sep 2007 14:42:14 -0300, Torsten Bronger
Yes, but I wasn't really sure how portable it is, in particular, on
Windows. So does

open(os.devnull, "w").write("Schallalla")

work on Windows like on Unix?

Yes. os.devnull=='nul' on Windows.
 
S

Sion Arrowsmith

Torsten Bronger said:
Yes, but I wasn't really sure how portable it is, in particular, on
Windows.

Windows has a NUL: device which behaves like /dev/null .
os.devnull is a wrapper around whatever the system-provided null
device is. I guess the documentation should reflect this, rather
than just mentioning POSIX and Mac OS 9 and requiring you to note
the "defined for all platforms" at the start of the section.
 
L

Lawrence D'Oliveiro

Windows has a NUL: device which behaves like /dev/null .

It's not a device, it's a reserved file name. Or rather, a reserved file
name prefix: any attempt to open a file name beginning with NUL, e.g.
NUL.DAT, will cause your output to disappear into a black hole.

This has bitten Windows users more than once.
 
A

Asun Friere

It's not a device, it's a reserved file name. Or rather, a reserved file
name prefix: any attempt to open a file name beginning with NUL, e.g.
NUL.DAT, will cause your output to disappear into a black hole.

Yes Windows and its whacky error messages. While trying to save a
Word doc as 'NUL.doc' I got this error message: "The file name
'NUL.doc' is a reserved device name." It's no wonder some might
mistake it for a device!
 
D

[david]

Lawrence said:
It's not a device, it's a reserved file name. Or rather, a reserved file
name prefix: any attempt to open a file name beginning with NUL, e.g.
NUL.DAT, will cause your output to disappear into a black hole.

This has bitten Windows users more than once.

copy myfile to NULL
copy myfile to NULfilename
copy myfile to NULprefix.txt

All work correctly creating files.

"Prefix" and "beginning with" are a bit misleading,
only a problem with file name NUL(.extension) (not
case sensitive unless case sensitive property is
enabled)

Not sure why you are calling it a reserved file name
instead of device, did not used to be, but many years
since I looked. Of course 'reserved file name' is a
bit misleading: you wouldn't copy bits to a reserved
file name.

Note NUL device used to be very very slow, again,
years since I looked.
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top