title: utf-8 codec can't decode byte <xxxx> in position <x>
body: <p>I have a function that read a sftp file, convert to byte and write to S3 as a S3 object. This function is a bit unreliable in a sense that when running the function on the same file, it works at times and fail at times.</p>
  <p>I have implemented the code in a way that it retries once it fail but the code would break if the function fails on the second try. I could add in more tries but is there anyway I could make the code more reliable?</p>
  <p>The function that I am using</p>
  <pre><code>def _download_file(sftp, file, s3, raw_bucket):
     
      # Get the zip file as a binary object
      r = BytesIO()
  
      try:
          sftp.getfo(file, r)
          r.seek(0)
          memfile = BytesIO(r.getvalue())
          if file.endswith('.zip'):
              print('{time} Downloading from {file} ...'.format(time=curr_time(), file=file))
              filepath = 'my_path/' + file
              logging.info('{time} Downloading from {file} ...'.format(time=curr_time(), file=filepath))
              s3.Object(raw_bucket, filepath).put(Body=memfile)
              print('{time} Downloaded target file: {file} ...'.format(time=curr_time(), file=file))
              return True
  
      except Exception as e:
              # on connection failure, report failure and try to connect again
              print(&quot;{time} {e}&quot;.format(time=curr_time(), e=e))
              print(&quot;{time} Unable to download {file}&quot;.format(time=curr_time(), file=file))
              return False
  </code></pre>
  <p>Errors I'm receiving</p>
  <pre><code>&quot;utf-8 codec can't decode byte &lt;xxxx&gt; in position &lt;x&gt;: invalid start byte&quot;
  &quot;utf-8 codec can't decode byte &lt;xxxx&gt; in position &lt;x&gt;: invalid continuation byte&quot;
  </code></pre>