简体   繁体   English

递归查找所有带有扩展名的文件

[英]Find all files with an extension recursively


I need to perform some automated action on a set of XML files.我需要对一组 XML 文件执行一些自动化操作。 I'm just learning Python so I've looked up for a similar SO answer and come up with this:我只是在学习 Python 所以我查找了类似的 SO 答案并想出了这个:

root_dir='/home/user/git/code'
for filename in glob.iglob(root_dir + '**/*.xml', recursive=True):
    print(filename)

The problem with the above code is that it finds just the top XML file which is on '/home/user/git/code' and not all those nested under that folder.上述代码的问题在于它只找到位于“/home/user/git/code”上的顶部 XML 文件,而不是所有嵌套在该文件夹下的文件。 The flag 'recursive' is set to true so I wonder what could be wrong with it.... Any idea? 'recursive' 标志设置为 true,所以我想知道它可能有什么问题......知道吗? Thanks谢谢

You forgot / between code and ** so you have code** instead of code/**您忘记/code**之间,所以您有code**而不是code/**

You need / at the end你需要/最后

 root_dir='/home/user/git/code/'

or at the beginning in或在开头

'/**/*.xml'

OR use os.path.join() instead of +或使用os.path.join()而不是+

os.path.join(root_dir, '**/*.xml')

I use this function endlessly for my own projects.我在自己的项目中无休止地使用这个 function。 Hope it can serve you well.希望它能很好地为您服务。

import os, glob

def get_files(path, extension, recursive=False):
    """
    A generator of filepaths for each file into path with the target extension.
    If recursive, it will loop over subfolders as well.
    """
    if not recursive:
        for file_path in glob.iglob(path + "/*." + extension):
            yield file_path
    else:
        for root, dirs, files in os.walk(path):
            for file_path in glob.iglob(root + "/*." + extension):
                yield file_path

Example: my_desktop_pdfs = list(get_files('users/xx/Desktop','pdf'))示例: my_desktop_pdfs = list(get_files('users/xx/Desktop','pdf'))

In your case:在你的情况下:

for f in get_files(root_dir, 'xml', recursive=True):
    print(f)

I don't know about glob.iglob but os.walk should produce the same result:我不知道glob.iglobos.walk应该产生相同的结果:

import os
for root, dirs, files in os.walk('/home/user/git/code'):
    for file in files:
        if (file.endswith('.xml')):
            print(file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python递归查找具有特定扩展名的文件 - Python recursively find files with specific extension 如何递归地列出具有特定扩展名的所有文件? - How to list all files with a certain extension recursively? 递归遍历tar文件并提取具有指定扩展名的所有文件 - Traverse tar file recursively and extract all files with specified extension 如何使用Python递归复制目录中具有特定扩展名的所有文件? - How to recursively copy all files with a certain extension in a directory using Python? 在python中递归查找文件 - Recursively find files in python 在 Python 中查找扩展名为 .txt 的目录中的所有文件 - Find all files in a directory with extension .txt in Python 如何按扩展名'filename_ext'.txt 附加文件并递归地添加到文件夹中的所有文件并将它们转换回原始扩展名 - How to append a file by extension 'filename_ext'.txt and recursively to all the files in a folder and convert them back into the original extension 递归查找列表的所有组合 - Recursively find all combinations of list 在Sphinx扩展中查找所有生成的HTML文件的名称 - Find names of all generated HTML files in Sphinx extension 找到所有匹配确切名称的文件,包含和不包含扩展名 - find all files matching exact name with and without an extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM