递归查找某个目录下是否存在NOTICE文件

从Catalogs.txt文件中,读取待检查的目录列表。检查这些目录中,是否存在NOTICE文件,如果没有则检查它的父目录,直到cd ..到Repository目录。

如果cd ..到Repository目录后,仍没有找到NOTICE文件,则说明这个目录中缺失了NOTICE文件,将这个目录打印出来,并存到result.txt文件中。

 1 # -*- coding: utf-8 -*-
 2 import os
 3 #从NOTICE获取待检查的目录列表
 4 def getCatalogs(file):
 5     files = open(file,'r')
 6     lines = files.readlines()
 7     return lines
 8 
 9 #检查单个目录及其父目录是否存在NOTICE文件
10 def scandir(initdir, checkName,stopName ):
11     dir = initdir + '/' + checkName
12     flag = os.path.exists(dir)
13     finalFlag = False
14     if flag :
15         return True
16     dir = os.path.dirname(initdir)
17     if stopName in str(dir):
18         finalFlag = scandir(dir, checkName,stopName )
19     return finalFlag
20 
21 def isExist(file, checkName, stopName):
22     catalogs = getCatalogs(file)
23     notContainCheckName= []
24     for line in catalogs:
25         line = line.strip()
26         line = line.strip('\n')
27         flag = scandir(line, checkName, stopName )
28         if(not flag):
29             notContainCheckName.append(line)
30     # 打印缺少NOTICE文件的目录
31     for line in notContainCheckName:
32         file = open('result.txt','a+')
33         file.write(line + '\n')
34         print(line)
35 
36 if __name__ == '__main__':
37     #Catalogs.txt:目录列表  NOTICE:待检查的文件  Repository:终止目录
38     isExist('Catalogs.txt', 'NOTICE', 'Repository')

 

posted @ 2016-07-26 23:58  信德维拉  阅读(426)  评论(1)    收藏  举报