我有一个包含多个文件的文件夹,而我正在寻找一个仅包含“低”和“ 1小时”的文件。因此,要读取所有这些文件地址,我正在使用glob库。
import glob
path = glob.glob("summary output\*.xlsx")
print(path)
输出:
summary output\2030 and 2030 High RE with 1 Hour storage.xlsx
summary output\2030 and 2030 High RE with 2 Hour storage.xlsx
summary output\2030 and 2030 High RE with 4 Hour storage.xlsx
summary output\2030 and 2030 High RE with 6 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 1 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 2 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 4 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 6 Hour storage.xlsx
这些是该文件summary output
夹中文件的列表。现在我想要一个包含Low
和的文件1 Hour
,基本上是这个文件:
summary output\2030 and 2030 Low RE with 1 Hour storage.xlsx
所以为此我写了这样的:
for file in path:
if "Low" and "1 Hour" in file:
print(file)
输出:
summary output\2030 and 2030 High RE with 1 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 1 Hour storage.xlsx
我不知道为什么要拿第一份文件,所以任何人都可以帮忙吗?
尝试这个
for file in path:
if "Low" in file and "1 Hour" in file:
print(file)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句