def prefixed(filename, i, digits):
    """Adds a number prefix to the filename, e.g. '0001_'.
    If the number of digits in 'i' is higher than 'digits',
    the number gets truncated.
    """
    s = str(i)
    prefix = "0"*(max(0,digits-len(s))) + s + "_"
    return prefix + filename