title: Create binary file in python
body: <p>I have a very large binary file called file1.bin and I want to create a file, file2.bin, that holds only the first 32kb of file1.bin.</p>
  
  <p>So I'm reading file1 as follows: </p>
  
  <pre><code>myArr = bytearray()
  
  with open(r"C:\Users\User\file1.bin", "rb") as f:
  byte = f.read(1)
  for i in range(32,678):
      myArr.extend(byte)
      byte = f.read(1)
  </code></pre>
  
  <p>My question is: How do I proceed from here to create the file2 binary file out of myArr?</p>
  
  <p>I tried </p>
  
  <pre><code>with open(r"C:\Users\User\file2.bin", "w") as f:
  f.write(myArr)
  </code></pre>
  
  <p>but this results in:</p>
  
  <pre><code>f.write(myArr)
  TypeError: must be string or pinned buffer, not bytearray
  </code></pre>