class FileInput(object):

    def __init__(self, filename=None, *args, **kwargs):
        self.file = open(filename, *args, **kwargs
            ) if filename and filename != '-' else sys.stdin

def __enter__(self):
    return self.file

def __exit__(self, type, value, traceback):
    if self.file is not sys.stdin:
        self.file.close()
Python use lxml with fileinput