[ANN] isrealfile function

Manlio Perillo NOmanlio_perilloSPAM at libero.it
Sat Jun 19 19:58:13 CEST 2004


On Fri, 18 Jun 2004 21:25:07 -0400, Peter Hansen <peter at engcorp.com>
wrote:

>Manlio Perillo wrote:
>
>> def isrealfile(file):
>>     """
>>     Test if file is on the os filesystem
>>     """
>>     
>>     if not hasattr(file, 'fileno'): return False
>> 
>>     try: tmp = os.dup(file.fileno())
>>     except: return False
>>     else: os.close(tmp); return True
>> 
>> I have implemented this function for testing if a Python script is
>> being executed by Python.exe or Pythonw.exe. In the latter case, in
>> fact, Microsoft implementation of stdout/stderr is broken.
>
>I haven't tested this idea, but wouldn't "os.isatty()" be
>the right thing for this purpose, instead of the os.dup()
>and os.close() stuff?
>

Yes, it works (but the documentation says that isatty is available
only on Unix!).

However there is a problem:

pythonw ascript.py > afile

In this case sys.stdout is not a tty but it is a real file (so I can
write on it).
isrealfile is only(?) useful for Microsoft Windows because 'pseudo'
stdout and stderr used by pythonw.exe are not real files and are
broken (if you write more than 4096 characters an exception is
raised!).

Probably a more logical solution is to simply close stdout and stderr.
However by connecting them to a NullStream is more useful (at least
for me).




Regards   Manlio Perillo



More information about the Python-list mailing list