import re


def cleanFilename(fname):
    """Turn runs of bad characters to have in a filename into a single underscore,
    remove any trailing underscore"""
    return re.sub("_$", "", re.sub("[ _\n\t/()*,&:;@.]+", "_", fname))