title: Change in tab indentation upon opening the file for writing
body: <p>I am reading a bunch of js files using Python. I am finding out the <code>tags:</code> section and updating it with additional values. My script is changing the previous indentation of the tags, especially for the closing bracket <code>]</code>.</p>
  <p>Here is what my loop looks like:</p>
  <pre><code>def update_js_file(filename: str, tags: list) -&gt; None:
      content = ''
      with open(filename, 'r', encoding='utf-8') as f:
          content = f.read()
  
      tags_raw = json.dumps(tags, indent=4)
      content = re.sub(r'tags: \[.+?\]', 'tags: ' + tags_raw, content, flags=re.DOTALL)
      with open(filename, 'w', encoding='utf-8') as f:
          f.write(content)
  </code></pre>
  <p>Git diff shows the following change in file:</p>
  <pre><code>     tags: [
  -      &quot;aws&quot;,
  -      &quot;cloudtrail&quot;,
  -      &quot;cap-04&quot;,
  -      &quot;mon-08&quot;
  -    ],
  +    &quot;aws&quot;,
  +    &quot;cloudtrail&quot;,
  +    &quot;cap-04&quot;,
  +    &quot;mon-08&quot;,
  +    &quot;NIST-800-171&quot;,
  +    &quot;NIST-800-171-3.3.8&quot;
  +],
  </code></pre>
  <p>How can I avoid this change in indentation for each tag and the closing bracket.</p>
  <p>Edit: I was able to fix the indentation by changing the value of indentation to 8 in <code>tags_raw</code> but I am still not able to properly indent the closing bracket <code>]</code>.</p>