我有了几行文本文件,让说:如何检查,看看是否在内容文件存在于目录中的文件中的内容
cat
dog
rabbit
我想遍历目录来检查是否有任何文本文件包含上述列表中的项目。
我尝试了许多不同的方法。我不想发布任何内容,因为我想要一个新的开始......思路清晰。我对下面的代码进行了处理,以至于我甚至不知道发生了什么,并且我完全失去了意义。 :(
#! /usr/bin/python
'''
The purpose of this program
is to search the OS file system
in order to find a txt file that contain the nagios host entries
'''
import os
host_list = open('/path/path/list', 'r')
host = host_list.read()
##for host in host_remove.read():
host_list.close()
#print host
for root, dirs, files in os.walk("/path/path/somefolder/"):
for file in files:
if file.endswith(".txt"):
check_file = os.path.join(root, file)
#print check_file
if host.find(check_file): #in check_file:
print host.find(check_file)
#print host+" is found in "+check_file
#print os.path.join(root, file)
else:
break
你的问题的第二部分完全是题外话。我删除它。 – MattDMo 2014-08-28 17:52:34
您可以分享您迄今为止的尝试吗? – dano 2014-08-28 18:46:41
第二个问题是公平的吗?但任何谁 – user3655020 2014-08-28 18:51:20