def check_extnum(filename):
    """Return extension number when given as filename.fits[extnum]

    Parameters
    ----------
    filename : string
        File name.

    Returns
    -------
    filename : string
        File name without the extension number (if initially present).
    extnum : int or None
        Extension number.

    """
    extnum = None
    if filename[-1] == ']':
        leftbracket = filename.rfind('[')
        if leftbracket != -1:
            cdum = filename[leftbracket + 1:-1]
            if len(cdum) > 0:
                try:
                    extnum = int(cdum)
                except ValueError:
                    raise ValueError('Invalid extension number {}'.format(
                        filename[leftbracket + 1:-1]))
                filename = filename[:leftbracket]
    return filename, extnum
