def open_if_filename(file_or_path, mode='rb'):
    """
    file_or_path can be either a string or a file-like object.
    if it is a string, a file will be opened with mode.
    Returns a tuple (file, should_be_closed).
    """
    if isinstance(file_or_path, str):
        return open(file_or_path, mode), True
    else:
        return file_or_path, False
