def write_file(filename, value, mode='w'):
    """Writes <value> to filename, making sure to close the file 
    explicitly on exit.
    """
    f = open(filename, mode)
    try:
        return f.write(value)
    finally:
        f.close()