'Open a FITS file from a filename string.'

def _open_filename(self, filename, mode, overwrite):

    if (mode == 'ostream'):
        self._overwrite_existing(overwrite, None, True)
    if os.path.exists(self.name):
        with fileobj_open(self.name, 'rb') as f:
            magic = f.read(4)
    else:
        magic = b('')
    ext = os.path.splitext(self.name)[1]
    if ((ext == '.gz') or magic.startswith(GZIP_MAGIC)):
        self._file = gzip.open(self.name, IO_FITS_MODES[mode])
        self.compression = 'gzip'
    elif ((ext == '.zip') or magic.startswith(PKZIP_MAGIC)):
        self._open_zipfile(self.name, mode)
    elif ((ext == '.bz2') or magic.startswith(BZIP2_MAGIC)):
        if (mode in ['update', 'append']):
            raise IOError('update    and    append    modes    are    not    supported    with    bzip2    files')
        bzip2_mode = ('w' if (mode == 'ostream') else 'r')
        self._file = bz2.BZ2File(self.name, bzip2_mode)
    else:
        self._file = fileobj_open(self.name, IO_FITS_MODES[mode])
        self.close_on_error = True
    if (isinstance(self._file, bz2.BZ2File) and (mode == 'ostream')):
        pass
    else:
        self._file.seek(0)
