Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python-mindepth&;中列出子目录中的文件;最大深度_Python_Os.walk - Fatal编程技术网

在Python-mindepth&;中列出子目录中的文件;最大深度

在Python-mindepth&;中列出子目录中的文件;最大深度,python,os.walk,Python,Os.walk,我想从根目录打印子目录中的文件,该子目录是内部的2级目录。在shell中,我可以使用下面的find命令 find -mindepth 3 -type f ./one/sub1/sub2/a.txt ./one/sub1/sub2/c.txt ./one/sub1/sub2/b.txt 在python中,如何实现这一点。我知道os.walk、glob和fnmatch的基本语法。但是不知道如何指定限制(如bash中的mindepeth和maxdepth)您不能将任何限制指定给。 但是,您可以编写一

我想从根目录打印子目录中的文件,该子目录是内部的2级目录。在shell中,我可以使用下面的find命令

find -mindepth 3 -type f
./one/sub1/sub2/a.txt
./one/sub1/sub2/c.txt
./one/sub1/sub2/b.txt

在python中,如何实现这一点。我知道os.walk、glob和fnmatch的基本语法。但是不知道如何指定限制(如bash中的mindepeth和maxdepth)

您不能将任何限制指定给。 但是,您可以编写一个函数来实现您想要的功能

import os
def list_dir_custom(mindepth=0, maxdepth=float('inf'), starting_dir=None):
    """ Lists all files in `starting_dir` 
    starting from a `mindepth` and ranging to `maxdepth`

    If `starting_dir` is `None`, 
    the current working directory is taken.

    """
    def _list_dir_inner(current_dir, current_depth):
        if current_depth > maxdepth:
            return
        dir_list = [os.path.relpath(os.path.join(current_dir, x))
                    for x in os.listdir(current_dir)]
        for item in dir_list:
            if os.path.isdir(item):
                _list_dir_inner(item, current_depth + 1)
            elif current_depth >= mindepth:
                result_list.append(item)

    if starting_dir is None:
        starting_dir = os.getcwd()

    result_list = []
    _list_dir_inner(starting_dir, 1)
    return result_list
编辑:添加了更正,减少了不必要的变量定义

第二次编辑:包括2条建议,使其列出与
find
完全相同的文件,即
maxdepth
是独占的

第三次编辑:通过2Ring添加了其他备注,还将路径更改为
relpath
,以与
find

相同的格式返回输出。您可以使用
.count()
方法查找深度:

import os

def files(rootdir='.', mindepth=0, maxdepth=float('inf')):
    root_depth = rootdir.rstrip(os.path.sep).count(os.path.sep) - 1
    for dirpath, dirs, files in os.walk(rootdir):
        depth = dirpath.count(os.path.sep) - root_depth
        if mindepth <= depth <= maxdepth:
            for filename in files:
                yield os.path.join(dirpath, filename)
        elif depth > maxdepth:
            del dirs[:] # too deep, don't recurse

.

看起来很像为什么你会在最大深度上使用浮点-你不能有一个级别的分数-我会使用一个默认值为-1的整数值并更改检查。@SteveBarnes:Fair point;我想float('inf')有点道理,因为没有一个等价的整数无穷大,尽管
sys.maxsize
可以工作。OTOH,我同意使用sentinel可能更好,但与使用-1相比,我会使用
None
,例如
如果maxdepth不是None,而current_depth>maxdepth:
@2Ring,您只需使用
current\u depth>=maxdepth
current\u depth>min\u depth
即可更改该值,以获得find的确切行为。@smcatepillar:差不多了!我想你需要
当前深度>=mindepth-1
。或者
current\u depth>=maxdepth
并用
\u list\u dir\u inner(starting\u dir,1)
调用它,啊,ok误解了mindepth的解释,我想,计数从0开始,而不是1意味着“处理除命令行参数以外的所有文件”。
 print('\n'.join(files(mindepth=3)))