[ANN] isrealfile function

Manlio Perillo NOmanlio_perilloSPAM at libero.it
Fri Jun 18 12:32:32 EDT 2004


Hi.
Maybe this function can be useful for someone:


import os

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.


[sitecustumize.py]
import sys

if not isrealfile(sys.__stdout__): sys.stdout = NullStream()
if not isrealfile(sys.__stderr__): sys.stderr = NullStream()


Where NullStream is a file like class that writes nothing.



P.S.
I have only tested this function on Windows XP Pro.


Regards   Manlio Perillo



More information about the Python-list mailing list