Monday, February 4, 2019

TypeError: 'str' does not support the buffer interface

Advertisements

I got this error while writing a script in python 3.4. On centos linux 7

The code was like this

  # Create temp file
  tempfile.tempdir = '/tmp'
  temp, temp_name = tempfile.mkstemp()
  if verbose:
    print(temp_name)
  os.write(temp,'Temporary file created by check_file_operations.py script for testing upload & download.\n')
  os.close(temp)

While running it raised the following error.
TypeError: 'str' does not support the buffer interface

it was because of the buffer text. python 3, the socket accepts bytes, you need to convert string to bytes with a encode() function like this:

  # Create temp file
  tempfile.tempdir = '/tmp'
  temp, temp_name = tempfile.mkstemp()
  if verbose:
    print(temp_name)
  os.write(temp,'Temporary file created by check_file_operations.py script for testing upload & download.\n'.encode())
  os.close(temp)

No comments:

Post a Comment

Be nice. That's all.