Is there a way to tell if a script has been run by Pythonw.exe instead of Python.exe?

BlueBird phil at freehackers.org
Fri Oct 19 12:00:28 CEST 2007


On Oct 18, 11:56 pm, Metalone <j... at iteris.com> wrote:
> In particular I want to know how to tell if reading and writing to the
> console can occur.
> Something like
> sys.isConsolePresent()

For a different problem, I have the following code. It might help:

def isrealfile(file):
	"""
	Test if file is on the os filesystem. This is necessary on windows,
when
        starting python with pythonw.exe because in that case, the
stdout and stderr
	are not real file and will create IOError when being flushed or when
more
	than 4096 bytes are written.
	"""
	if not hasattr(file, 'fileno'): return False
	try: tmp = os.dup(file.fileno())
	except: return False
	else: os.close(tmp); return True

class NullStream:
    """
    A file like class that writes nothing
    """
    def close(self): pass
    def flush(self): pass
    def write(self, str): pass
    def writelines(self, sequence): pass

if not isrealfile(sys.stdout):
   sys.stdout = NullStream()

if not isrealfile(sys.stderr):
   sys.stderr = NullStream()





More information about the Python-list mailing list