【发布时间】:2020-06-21 18:11:18
【问题描述】:
我在使用 Python3 时遇到以下错误,但该函数在 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
在这里进行什么最安全的更改,以免破坏任何现有的东西?
多语言的答案表示赞赏。
【问题讨论】:
标签: python python-3.x