title: HTML file size changes after reading and writing to a new file using beautifulsoup
body: <p>I have an HTML file <a href="https://drive.google.com/file/d/1YMLeZDSKpURGAcWITWbhDBDYLOTiLRP7/view?usp=sharing" rel="nofollow noreferrer">here</a> of File size - <strong>1128 KB</strong>.</p>
  <p>Following shows the size of the above HTML file:
  <a href="https://i.stack.imgur.com/NYyyB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NYyyB.png" alt="enter image description here" /></a></p>
  <p>Following is my code to just read and write the HTML file to a new file <code>sample.htm</code>:</p>
  <pre><code>from bs4 import BeautifulSoup
  
  #path to HTML file
  html = r'C:/Users/USER/Downloads/form10-ka.htm'
  with open(html, 'r',encoding='utf-8') as f:
      contents = f.read()
      soup  = BeautifulSoup(contents, 'html.parser')
      
  with open(&quot;sample.htm&quot;, &quot;w&quot;, encoding='utf-8') as file:
      file.write(str(soup))
  </code></pre>
  <p>The above code generates a <code>sample.htm</code> as below:
  <a href="https://i.stack.imgur.com/vgoWQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vgoWQ.png" alt="enter image description here" /></a></p>
  <p>File size - <strong>1082 KB</strong>.
  The same HTML when written to a new file changes the file size. It seems the <code>&amp;nbsp;</code> and a few other characters are not preserved.</p>
  <p>I tried another code to preserve the <code>&amp;nbsp;</code> as below:</p>
  <pre><code>with open(&quot;sample-1.htm&quot;, &quot;w&quot;, encoding='utf-8') as file:
       file.write(str(soup.encode(formatter=&quot;html&quot;).decode())) 
  </code></pre>
  <p>The above code generates a new file <code>sample-1.htm</code> as below:
  <a href="https://i.stack.imgur.com/z9QUh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z9QUh.png" alt="enter image description here" /></a></p>
  <p>Filesize - <strong>1106 KB</strong>.
  This preserves the <code>&amp;nbsp;</code> and yet again is <strong>lesser than the original file size</strong></p>
  <p>What else can be done to maintain the original file size(An increase in file size should also work) !!</p>
  <p>Any leads would be helpful</p>