Changing a value in a yaml file using Python
<p>I have a <code>.yaml</code> file I want to update with Python code. Let's say it looks something like that:</p>
<pre><code>  state: 'present'
</code></pre>
<p>I'd like to have a code that changes the state and saves the file. I'm trying with something like this and fail:</p>
<pre><code>def set_state(state):
    with open(&quot;file_to_edit.yaml&quot;, 'rw') as f:
        doc = yaml.load(f)
    doc['state'] = state
    yaml.dump(f)
</code></pre>
<p>I am using the 'yaml' package for Python.</p>