Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.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 使用包含递归的选项筛选文件列表_Python_List_File_Recursion - Fatal编程技术网

Python 使用包含递归的选项筛选文件列表

Python 使用包含递归的选项筛选文件列表,python,list,file,recursion,Python,List,File,Recursion,我试图编写几行代码,允许用户请求提供目录中的文件列表,但也允许递归。我发现这个答案非常接近我想要的,但它不处理递归。我使用的是Python2.7.x import glob print glob.glob( '\myfavoritePath\' ) 下面的这些示例将从给定路径返回一个经过筛选的文件列表 # basically providing same answers as OS would when doing a Dir getFileList ( "/MyFavoritepPath/"

我试图编写几行代码,允许用户请求提供目录中的文件列表,但也允许递归。我发现这个答案非常接近我想要的,但它不处理递归。我使用的是Python2.7.x

import glob
print glob.glob( '\myfavoritePath\' )
下面的这些示例将从给定路径返回一个经过筛选的文件列表

# basically providing same answers as OS would when doing a Dir
getFileList ( "/MyFavoritepPath/" )
getFileList ( "/MyFavoritepPath/*.txt" )
getFileList ( "/MyFavoritepPath/myFile*.txt" )

这些示例包括根目录的结果以及给定文件夹中包含的路径

getFileList ( "/MyFavoritepPath/ -r" )
getFileList ( "/MyFavoritepPath/*.txt -r" )
getFileList ( "/MyFavoritepPath/myFile*.txt -r" )

最后一个要求是,这只需要几行代码。保持这个简单是非常重要的。它应该包括与给定路径匹配的所有文件和文件夹的完整列表。

仅供参考,但是glob.glob“\myfavoritePath\”由于反斜杠处理不当而无法工作。谢谢,这只是我的原始文本的复制粘贴错误。我从提供的根文件夹中获取结果。我就是不能让递归工作。这就是我希望得到一些帮助的地方。我也看过os.listdirpath,但文档中没有提到处理文件过滤器或递归。我还没有机会尝试,有时间我会尝试的。这将是很好的,在这里从任何有直接经验的这个功能以及。通常API文档会给出它可以处理的示例,所以我认为它不支持它。
# basically providing same answers as OS would when doing a Dir
getFileList ( "/MyFavoritepPath/" )
getFileList ( "/MyFavoritepPath/*.txt" )
getFileList ( "/MyFavoritepPath/myFile*.txt" )