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