code: def safe_print(text, file=sys.stdout, flush=False):
      
      if not isinstance(text, basestring):
          return print(text, file=file)
      try:
          file.write(text)
      except UnicodeEncodeError:
          bytes_string = text.encode(file.encoding, 'backslashreplace')
          if hasattr(file, 'buffer'):
              file.buffer.write(bytes_string)
          else:
              text = bytes_string.decode(file.encoding, 'strict')
              file.write(text)
      file.write("\n")
docs: Prints a (unicode) string to the console, encoded depending on
      the stdout/file encoding (eg. cp437 on Windows). This is to avoid
      encoding errors in case of funky path names.
      Works with Python 2 and 3.