title: Exception in thread Thread-10 error in write function, TypeError: write() argument must be str, not bytes
body: <p>Data coming to this tcp server is not write into file as well as on command line.</p>
  
  <p>Error is as below,</p>
  
  <blockquote>
    <p>Exception in thread Thread-10: Traceback (most recent call last): File
    "F:\Installation\Anaconda\lib\threading.py",  line 916, in
    _bootstrap_inner self.run() File "", line 25, in run f.write(data) TypeError: write() argument must be str,
    not bytes</p>
  </blockquote>
  
  <p>My code:</p>
  
  <pre><code>import socket
  from threading import Thread
  from socketserver import ThreadingMixIn
  import time
  TCP_IP = '192.168.0.159'
  TCP_PORT = 9001
  BUFFER_SIZE = 1024
  
  class ClientThread(Thread):
      def __init__(self,ip,port,sock):
          Thread.__init__(self)
          self.ip = ip
          self.port = port
          self.sock = sock
          print(" New thread started for "+ip+":"+str(port))
  
      def run(self):
          filename='ble_scan.txt'
          f = open(filename,'w')
          while True:
              data=self.sock.recv(BUFFER_SIZE)
              print((data))
              f.write(data)
              f.close()
              self.sock.close()
              break
  
  tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  tcpsock.bind((TCP_IP, TCP_PORT))
  threads = []
  while True:
      tcpsock.listen(5)
      print("Waiting for incoming connections...")
      (conn, (ip,port)) = tcpsock.accept()
      print('Got connection from ', (ip,port))
      newthread = ClientThread(ip,port,conn)
      newthread.start()
      threads.append(newthread)
  for t in threads:
      t.join()
  
  </code></pre>