1

When I run this code:

    stdout = Popen(callbackquery, shell=True, stdout=PIPE).stdout
    output = stdout.read()
    if output.find("No records were found that matched the search criteria") == -1:
        print(output)
    else:
        # Do nothing
        print("It's fine")

I get the following error:

   if output.find("No records were found that matched the search criteria") == -1:
TypeError: 'str' does not support the buffer interface

I understand that this is to do with character encoding but I don't know where and how I need to convert this?

3
  • Are you using python 2.x or 3? Commented Aug 7, 2015 at 19:04
  • Have you Googled that error along with "popen", "stdout.read", and so on? There are several existing questions with answers that might help you. Commented Aug 7, 2015 at 19:04
  • stdout = Popen(callbackquery, shell=True, stdout=PIPE, universal_newlines=True).stdout output = stdout.read() Commented Aug 7, 2015 at 19:16

3 Answers 3

1

For people who are wondering why this issue occured. From subprocess documentation -

If universal_newlines is True, the file objects stdin, stdout and stderr are opened as text streams in universal newlines mode, as described above in Frequently Used Arguments, otherwise they are opened as binary streams.

The default value for universal_newlines is False, which means that stdout is a binary stream, and the data it returns is a byte string.

And the issue occurs because we are trying to do a .find() on the byte string passing in string as argument. A very simple example to show this -

>>> b'Hello'.find('Hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface

You should .decode() the data , Example -

stdout = Popen(callbackquery, shell=True, stdout=PIPE, universal_newlines=True).stdout
output = stdout.read.decode('<encoding>') #The encoding with which the output of the other process is returned, can be something like utf-8, etc.
Sign up to request clarification or add additional context in comments.

Comments

0
If  "No records were found that matched the search      criteria" in output:
  Do something 

1 Comment

that gives the same error. It is to do with UTF encoding
0
stdout = Popen(callbackquery, shell=True, stdout=PIPE, universal_newlines=True).stdout
    output = stdout.read()

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.