首页 > 解决方案 > 无法让 findstr 与在 Windows 上运行的子进程一起工作

问题描述

我想从我的 python 环境中运行 findstr,但在使用 subprocess 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 才能工作)。

关于如何让它发挥作用的任何想法?不知道我错过了什么。非常感谢。

标签: pythonwindowssubprocessfindstr

解决方案


推荐阅读