I am getting following error with Python3 but the function works fine with Python2
from subprocess import Popen, PIPE, STDOUT
import re
def source_shell():
pattern = re.compile('^(w+)=(.*)$')
cmd = 'ls /etc/fstab /etc/non-existent-file'
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
for line in p.stdout:
line = line.strip()
if not pattern.match(line):
print("hurray")
source_shell()
Traceback (most recent call last):
File "main.py", line 15, in <module>
source_shell()
File "main.py", line 12, in source_shell
if not pattern.match(line):
TypeError: cannot use a string pattern on a bytes-like object
What is the safest change to make here, so that it does not break any existing things?
Polyglot answer is appreciated.