<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _update_pretty_hostname(new_val: str):
 """ Write a new value for the pretty hostname. :raises OSError: If the new value could not be written. """

    try:
        with open('/etc/machine-info') as emi:
            contents = emi.read()
    except OSError:
        LOG.exception("Couldn't read /etc/machine-info")
        contents = ''
    new_contents = ''
    for line in contents.split('\n'):
        if not line.startswith('PRETTY_HOSTNAME'):
            new_contents += f'{line}\n'
    new_contents += f'PRETTY_HOSTNAME={new_val}\n'
    with open('/etc/machine-info', 'w') as emi:
        emi.write(new_contents)