def leading_zeros(string, delimiter='_', n_digits=3, exclude=None):
    """ 
    Returns a new string with added leading zeros.
    Parse string and for every section (as is 
    indicated by delimiter) try to convert it to an
    integer and if possible, add zeros (n_digits). 
    It is possible to exclude part of the string 
    (e.g. the file extension) by passing a string 
    "exclude". Only the part of the string up to the 
    first occurance of that string is parsed.    
    """
    
    if exclude:
        fname = string[:string.find(exclude)]
        ext = exclude
    else:
        fname = string
        ext = ''
    
    fname_info = fname.split(delimiter)
    
    for i, info in enumerate(fname_info):                
        try: 
            # try converting string to int
            num = int(info)
                    
            # if succeeded convert int to string with n_digits
            nnum = '%0' + str(n_digits) + 'd'
            nnum %= num
            
            # change string entry
            fname_info[i] = nnum
        except:
            pass
        
    return '_'.join(fname_info) + ext