471,305 Members | 1,250 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

In particular I want to know how to tell if reading and writing to the
console can occur.
Something like
sys.isConsolePresent()

Oct 18 '07 #1
5 1333
On Oct 19, 7:56 am, Metalone <j...@iteris.comwrote:
In particular I want to know how to tell if reading and writing to the
console can occur.
Something like
sys.isConsolePresent()
Have you tried:

sys.stdin.isatty()
sys.stdout.isatty()

Graham

Oct 18 '07 #2
On Oct 18, 2:56 pm, Metalone <j...@iteris.comwrote:
In particular I want to know how to tell if reading and writing to the
console can occur.
Something like
sys.isConsolePresent()
Look at sys.executable to find the name of the binary for the Python
interpreter.

Oct 19 '07 #3
On Oct 18, 11:56 pm, Metalone <j...@iteris.comwrote:
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()
Oct 19 '07 #4
Thanks to all, I learned something in each post.
When using py2exe to build an executable sys.executable does not
provide the name of the python interpreter but the name of the
executable generated by py2exe.

Oct 19 '07 #5
Metalone schrieb:
Thanks to all, I learned something in each post.
When using py2exe to build an executable sys.executable does not
provide the name of the python interpreter but the name of the
executable generated by py2exe.
When running the executable built with py2exe you might be interested
in the variable sys.frozen; they are set to the string 'console' or 'windows', IIRC.

Thomas

Oct 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Guyon Morée | last post: by
8 posts views Thread by Thomas Aanensen | last post: by
reply views Thread by Nicola Larosa | last post: by
1 post views Thread by Charles Stricklin | last post: by
3 posts views Thread by Brainsludge | last post: by

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.