import gzip
import bz2


def open_file(file_name, flags='r'):
    """Opens a regular or compressed file (decides on the name)

      :param  file_name a name of the file, it has a '.gz' or
              '.bz2' extension, we open a compressed stream.
      :param  flags    open flags such as 'r' or 'w'
    """
    if file_name.endswith('.gz'):
        return gzip.open(file_name, flags)
    elif file_name.endswith('.bz2'):
        return bz2.open(file_name, flags)
    else:
        return open(file_name, flags)