1

I have a python script. It runs ok in python2, but when I run it in python3, I get this error:

name = r.content.translate(None, "\n \t/\"'")
TypeError: a bytes-like object is required, not 'str'

the line that assigned r is this:

r = requests.get('10.10.10.10/test_' + name, verify=True)

how can I fix this problem ? I need my script to run in python3.

2 Answers 2

1

You need to use

name = (r.content.translate(None, "\n \t/\"'".encode())).decode
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but that created a new problem couple of lines later: TypeError: cannot use a string pattern on a bytes-like object. The line that triggert the error is: m = re.search('test-(\d+.\d+.\d+)-\d+.patch' , name)
@MartinVegter Oh, ok, You should do name = (r.content.translate(None, "\n \t/\"'".encode())).decode() to get the string, or name = str(r.content.translate(None, "\n \t/\"'".encode()))
0

Not sure about what translate function needs. you must check the documentation. But looking at the error may the following might fix the error.

name = r.content.translate(None, b"\n \t/\"'")

name is then of type bytes. so you need to convert it back to type str.

So you can try

name = str(r.content.translate(None, b"\n \t/\"'"))

2 Comments

thanks, that fixed that particular problem. But it created a new one couple of lines later: TypeError: can only concatenate str (not "bytes") to str. The line that triggered this error is: r = requests.get(baseurl + name, auth=(username, password), verify=True).
updated the answer. updated the type from bytes to str

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.