'Reads the first few bytes from file_name and tries to guess the text
encoding. Returns None as a guess if it can\'t detect it.'

def _DetectInputEncoding(self, file_name):

    fp = open(file_name, 'rb')
    try:
        header = fp.read(3)
    except e:
        fp.close()
        return None
    fp.close()
    if header.startswith('\xfe\xff'):
        return 'UTF-16'
    elif header.startswith('\xff\xfe'):
        return 'UTF-16'
    elif header.startswith('\xef\xbb\xbf'):
        return 'UTF-8'
    else:
        return None
