title: Wierd null characters when writing to a text file
body: <p>I was writing a function that replaces a single line in a text file to a user inputted one.</p>
  <pre><code>def file_redact(file_name: str):
      line_to_replace = int(input('Enter the line to replace (starting from 1): '))
      f = open(file_name, 'r+')
      file_data = f.readlines()
      file_data[line_to_replace - 1] = input('Write a new line:\n') + '\n'
      f.truncate(0)
      f.writelines(file_data)
      f.close()
  </code></pre>
  <p>The function does this just fine, however, it also adds null characters to the beginning of the file.</p>
  <p>This is how the file looks like after I've replaced the first line:</p>
  <pre><code>\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00Replaced text
  
  Sample text2
  
  Sample text3
  
  Sample text4
  
  Sample text5
  </code></pre>
  <p>Any help is appreciated, especially if there's a better way to implement this.</p>