Python等价于“find-typef”

Python等价于“find-typef”,python,file,search,python-3.x,recursion,Python,File,Search,Python 3.x,Recursion,在Python3中,与bash命令find-typef等效的是什么 将生成一个如下所示的列表: /etc/rsyslog.conf /etc/request-key.d/cifs.idmap.conf /etc/request-key.d/id_resolver.conf /etc/issue /etc/maven/maven2-depmap.xml /etc/gtkmathview/gtkmathview.conf.xml /etc/fstab /etc/machine-id /etc/rpm

在Python3中,与bash命令find-typef等效的是什么

将生成一个如下所示的列表:

/etc/rsyslog.conf
/etc/request-key.d/cifs.idmap.conf
/etc/request-key.d/id_resolver.conf
/etc/issue
/etc/maven/maven2-depmap.xml
/etc/gtkmathview/gtkmathview.conf.xml
/etc/fstab
/etc/machine-id
/etc/rpmlint/mingw-rpmlint.config
/etc/rpmlint/config
/etc/cupshelpers/preferreddrivers.xml
/etc/pulse/system.pa
/etc/pulse/daemon.conf
/etc/brltty.conf
/etc/numad.conf
...
在Python3中,如何在指定路径下递归地获取所有文件(不包括目录)的列表?我还希望路径的标题镜像输入的路径。例如,如果我在/etc中运行find-类型f我会得到一个如下列表:

./rsyslog.conf
./request-key.d/cifs.idmap.conf
...
区别在于/etc/。。。vs./…

您可以查看每个文件,然后使用检查类型。那会让你很接近

import os
import os.path

for root, dirs, files in os.walk('/path/to/directory'):
    for f in files:
        fname = os.path.join(root, f)
        if os.path.isfile(fname):
            print fname  # or do something else with it...
我不确定你的问题中的/etc vs../是怎么回事,但我怀疑如果这不是你想要的,那么你只需要做一些类似的事情

os.path.relpath(fname, '/path/to/directory')
获取所需的相对路径。

您可以查看每个文件,然后使用检查类型。那会让你很接近

import os
import os.path

for root, dirs, files in os.walk('/path/to/directory'):
    for f in files:
        fname = os.path.join(root, f)
        if os.path.isfile(fname):
            print fname  # or do something else with it...
我不确定你的问题中的/etc vs../是怎么回事,但我怀疑如果这不是你想要的,那么你只需要做一些类似的事情

os.path.relpath(fname, '/path/to/directory')

要获取所需的相对路径。

可以使用子流程在python中执行system命令:

import subprocess
output=subprocess.check_output(['find /etc/ -type f'])
print output
或使用命令模块:

import commands
output=commands.getstatusoutput('find /etc/ -type f')

您可以使用以下子流程在python中执行系统命令:

import subprocess
output=subprocess.check_output(['find /etc/ -type f'])
print output
或使用命令模块:

import commands
output=commands.getstatusoutput('find /etc/ -type f')
我相信glob就是你要找的。我相信glob就是你要找的。