我正在尝试使用文件路径填充嵌套字典。首先,我为集群中的每个节点解压缩文件包。然后,我有一个为每个zip文件创建字典的函数。 这是针对python 3.8的。
结构如下:
ZipFile_2020-01-22-09-11-24_2020-01-22T14_56_54.905 {'node': None, 'xml': None, 'perops': None, 'access': None, 'logs': None}
现在,我正在尝试查找路径并将其添加到xml文件。我可以找到文件,也可以填充嵌套字典,但是它是:<generator object _iglob at 0x7f9c544bfba0>
。
处理此问题的最佳方法是什么?如何将生成器对象转换为文件路径?
功能如下:
def nodefile():
supportzips = {}.fromkeys(list((filter(lambda x: os.path.isdir(x), os.listdir('.')))),{}.fromkeys(list(['node','xml','perops','access','logs'])))
for k,v in supportzips.items():
supportzips[k]['xml'] = glob.iglob('./**/application-properties/application.xml')
return supportzips
输出:
Bitbucket_support_2020-01-22-09-11-24_2020-01-22T14_56_54.905 {'node': None, 'xml': <generator object _iglob at 0x7f9c544bfba0>, 'perops': None, 'access': None, 'logs': None}
<generator object _iglob at 0x7f9c544bfba0>
Bitbucket_support_2020-01-22-09-14-11_2020-01-22T14_56_49.965 {'node': None, 'xml': <generator object _iglob at 0x7f9c544bfba0>, 'perops': None, 'access': None, 'logs': None}
<generator object _iglob at 0x7f9c544bfba0>
Bitbucket_support_2020-01-22-09-11-57_2020-01-22T14_56_53.231 {'node': None, 'xml': <generator object _iglob at 0x7f9c544bfba0>, 'perops': None, 'access': None, 'logs': None}
<generator object _iglob at 0x7f9c544bfba0>
Bitbucket_support_2020-01-22-09-12-49_2020-01-22T14_56_51.505 {'node': None, 'xml': <generator object _iglob at 0x7f9c544bfba0>, 'perops': None, 'access': None, 'logs': None}
<generator object _iglob at 0x7f9c544bfba0>```
答案 0 :(得分:0)
应该{{1}}函数以一种内存有效的方式使用,即在需要评估之前它不会在内存中创建数据。从文档中:
返回一个迭代器,该迭代器产生的值与glob()相同,而实际上并没有同时存储它们。
如果您对glob.iglob()
的调用真正期望的是单个文件,则最好使用iglob
来返回glob
个匹配项,或者通过将其括起来对生成器进行强制评估可以在list
或list
构造函数中使用。
tuple