def streamprint(iterator, filehandle, bufferlength=10000, sep='\n'):
    """Given an iterator of lines and a filehandle, prints the content of the
    iterator to the file in a memory-efficient way.
    
    Return the number of iterator-given strings processed."""
    
    buffer = list()
    operations = 0
    
    for thing in iterator:      
        buffer.append(str(thing))
        
        if len(buffer) == bufferlength:
            print(sep.join(buffer), file=filehandle, end=sep)
            operations += bufferlength
            buffer.clear()
    
    print(sep.join(buffer), file=filehandle, end=sep)
    return operations + len(buffer)