我有一个目录列表和一个文件名。我想检查这个文件名是否存在于任何目录中,如果存在则代码通过,否则我们将出错。不过,我在下一步中遇到了一些麻烦,这就是我所拥有的。
for value in d.values():
for path in value:
if exists(path + '/' + filename):
print 'True'
else:
print 'False'
这是我的测试输出列表:
False
False
False
False
False
False
False
False
False
False
False
False
False
False
True
True
True
True
如您所见,这失败了很多,但这里有一些通过的情况,一次通过的事实对我来说已经足够了,但我不确定如何将其转换为正确的 python 逻辑。
基本上我想做的是
if file exists in any list_of_paths:
print 'True'
else:
print 'False'
任何人都可以建议如何去做吗?
请您参考如下方法:
import os
print(any(os.path.exists(os.path.join(dirpath, filename))
for directories in d.values()
for dirpath in directories))
