pythonpython-requestspython-3.8

Error :cannot use a string pattern on a bytes-like object


Hy am using Python RegEx to show all internet wirless profiles connected to a computer.There is error (TypeError: cannot use a string pattern on a bytes-like object) in my Second last line pls anyone help to identifi my mistake.Thanks

My Program

import subprocess,re
command = "netsh wlan show profile"
output = subprocess.check_output(command, shell=True)  
network_names = re.search("(Profile\s*:\s)(.*)", output)  
print(network_names.group(0))

.....................................................

ERROR

line 8, in <module>


 return _compile(pattern, flags).search(string)


TypeError: cannot use a string pattern on a bytes-like object

Solution

  • Python 3 distinguishes "bytes" and "string" types; this is especially important for Unicode strings, where each character may be more than one byte, depending on the character and the encoding.

    Regular expressions can work on either, but it has to be consistent — searching for bytes within bytes, or strings within strings.

    Depending on what you need, there are two solutions: