我想从我的python环境中运行findstr,并且在使用子进程run调用时无法使它正常工作(我可以使用dos提示符从命令行运行findstr命令):
commandlist = ['findstr', '/s', '"texttofind"', '*.py']
cwd = 'e:\\python'
shell = True
encoding = 'utf-8'
output = subprocess.run(commandlist,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding=encoding, shell=shell, cwd=cwd)
的返回码为1:
CompletedProcess(args=['findstr', '/s', '"runrcode"', '*.py'],returncode=1, stdout='', stderr='')
如果我改用commandlist = ['path'],那么一切正常,导致返回码0和stdout以及预期的路径文本列表(当然,shell必须为True才能起作用)。
有关如何使其正常工作的任何想法?不知道我在想什么。非常感谢。