Python 获取查找:路径必须位于子进程的表达式错误之前。检查\u输出

Python 获取查找:路径必须位于子进程的表达式错误之前。检查\u输出,python,python-3.x,Python,Python 3.x,我正在尝试获取目录中的文件数。代码如下: import subprocess op = subprocess.check_output(['find', '/home/my-path', '-type', 'f', '|', 'wc', '-l']) print(op) 我遇到以下错误: find: paths must precede expression: | Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|ra

我正在尝试获取目录中的文件数。代码如下:

import subprocess
op = subprocess.check_output(['find', '/home/my-path', '-type', 'f', '|', 'wc', '-l'])
print(op)
我遇到以下错误:

find: paths must precede expression: |
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
Traceback (most recent call last):
  File "chk.py", line 42, in <module>
    op = subprocess.check_output(['find', '/ml/cadv1/nipatel/copied-repo', '-type', 'f', '|', 'wc', '-l'])
  File "/ml/tools/opensource/python3/lib/python3.5/subprocess.py", line 629, in check_output
    **kwargs).stdout
  File "/ml/tools/opensource/python3/lib/python3.5/subprocess.py", line 711, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['find', '/home/my-path', '-type', 'f', '|', 'wc', '-l']' returned non-zero exit status 1
find:路径必须位于表达式之前:|
用法:find[-H][L][P][Olevel][D help | tree | search | stat | rates | opt | exec][path…][expression]
回溯(最近一次呼叫最后一次):
文件“chk.py”,第42行,在
op=子流程。检查_输出(['find'、'/ml/cadv1/nipatel/copied repo'、'-type'、'f'、'|'、'wc'、'-l'])
文件“/ml/tools/opensource/python3/lib/python3.5/subprocess.py”,第629行,在check_输出中
**kwargs)stdout
文件“/ml/tools/opensource/python3/lib/python3.5/subprocess.py”,第711行,正在运行
输出=标准输出,标准输出=标准输出)
subprocess.CalledProcessError:命令“['find'、'/home/my path'、'-type'、'f'、'|'、'wc'、'-l']”返回非零退出状态1

有人能在这里告诉我这个问题吗?

你不能像那样用
子流程进行管道输送。
相反,您可以读取find的
stdout
,然后将其作为
stdin
传递给带有
wc-l
的新
Popen

import subprocess
term = subprocess.Popen(['find', '/home/my-path', '-type', 'f'], stdout=subprocess.PIPE)
find_out = term.communicate()[0]
term2 = subprocess.Popen(['wc', '-l'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
op = term2.communicate(find_out)[0].decode()
print(op)
详细解释:

首先,我们用
subprocess.Popen
打开一个终端会话。然后我们使用
subprocess.PIPE
重定向输入/输出,否则,它将打印结果。要访问它们,我们可以使用stdout,stderr=term.communicate(stdin)
。我们重复将数据传递到
wc

注意,当我们已经有python中的数据时,我们还可以计算python中的行数

import subprocess
term = subprocess.Popen(['find', '/home/my-path', '-type', 'f'], stdout=subprocess.PIPE)
find_out = term.communicate()[0]
print(len(find_out.decode().splitlines()))

您不能像那样使用
子流程
进行管道传输。
相反,您可以读取find的
stdout
,然后将其作为
stdin
传递给带有
wc-l
的新
Popen

import subprocess
term = subprocess.Popen(['find', '/home/my-path', '-type', 'f'], stdout=subprocess.PIPE)
find_out = term.communicate()[0]
term2 = subprocess.Popen(['wc', '-l'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
op = term2.communicate(find_out)[0].decode()
print(op)
详细解释:

首先,我们用
subprocess.Popen
打开一个终端会话。然后我们使用
subprocess.PIPE
重定向输入/输出,否则,它将打印结果。要访问它们,我们可以使用stdout,stderr=term.communicate(stdin)
。我们重复将数据传递到
wc

注意,当我们已经有python中的数据时,我们还可以计算python中的行数

import subprocess
term = subprocess.Popen(['find', '/home/my-path', '-type', 'f'], stdout=subprocess.PIPE)
find_out = term.communicate()[0]
print(len(find_out.decode().splitlines()))