1
votes

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

2 Answers

1
votes

You need to use

name = (r.content.translate(None, "\n \t/\"'".encode())).decode
0
votes

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/\"'"))