title: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 1: invalid continuation byte
body: <p>I'm trying to practice reading text from a file in python 3.5, but I keep getting an error message.</p>
  
  <p>Here's my code:</p>
  
  <pre><code># A program that reads a text file and prints its contents on the screen
  # so that each row contains a line number
  # A reminder for UTF-8 users:
  
  
  def insert_line_number(line, line_number):
      """
      A function that takes a line and a corresponding line number and joins them
      together.
  
      :param line: A line of text
      :param line_number: A corresponding line number
      :return: A line with teh line number at the beginning
      """
      line_with_number = "{:3d} {:s}".format(line_number, line)
      return line_with_number
  
  
  def print_lines(file):
      """
      A function that reads lines from a file and prints them with an added line
      number.
  
      :param file: The file whose lines are to be printed
      :return: -
      """
      line_number = 0
      try:
          # Going through the lines, adding line numbers and printing the
          # resulting line
          for line in file:
              # Removing unnecessary characters from the end of the line
              line = line.rstrip()
              line_number += 1
              # Creating a line with the appropriate line number
              line_with_number = insert_line_number(line, line_number)
              print(line_with_number)
      except IOError:
          print("Error reading the file. Program will close.")
  
  
  # Our main
  def main():
      filename = input("Syötä tiedoston nimi: ")
      try:
          file = open(filename, "r", encoding="utf-8")
          print_lines(file)
          file.close()
      except IOError:
          print("Error opening the file. Program will close.")
  
  
  # Program start
  main()
  </code></pre>
  
  <p>...and here's the error message:</p>
  
  <pre><code>Traceback (most recent call last):
    File "F:/.../5.7.1(tiedostojen_lukeminen).py", line 55, in &lt;module&gt;
      main()
    File "F:/.../5.7.1(tiedostojen_lukeminen).py", line 48, in main
      print_lines(file)
    File "F:/.../5.7.1(tiedostojen_lukeminen).py", line 32, in print_lines
      for line in file:
    File "C:\Program Files (x86)\Python35-32\lib\codecs.py", line 321, in decode
      (result, consumed) = self._buffer_decode(data, self.errors, final)
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 1: invalid continuation byte
  
  Process finished with exit code 1
  </code></pre>
  
  <p>Thomas K had this to say in another thread:</p>
  
  <blockquote>
    <p>"To put the same thing another way: when you open a file for reading,
    the encoding parameter needs to be the encoding the file is already
    in, not the encoding you want (you select that when you write the
    file)."
     – Thomas K Nov 20 '12 at 13:02 </p>
  </blockquote>
  
  <p>Before posting this here I was specifically instructed to use the encoding="utf-8" -parameter inside the open()-function. Without the parameter the code runs just fine on my machine, but if I include it, I get the error above.</p>
  
  <p>I'd be interested to know what is going on behind the scenes. Why doesn't my code work when the parameter is included?</p>