Python 遍历文件,字符串操作

写一个简单的脚本,循环遍历单层文件夹,检查源代码中是否有一些特殊的类。

import os
import codecs

dirroot = "......"
line_num = 0   

def ContainsSpecialWords(line):
    for w in ["Double","Integer","Float","Long","Date"]:
        if w in line:
           if "//" not in line:
              return True
    return False

for parent,dirnames,filenames in os.walk(dirroot):   
    print("enter: " + dirroot)  
    for filename in filenames:  
        file = dirroot + filename  
        print("read " + file)  
        fin = codecs.open(file,'r','utf-8')
        lineNumber = 0
        for lines in fin:  
            lineNumber = lineNumber + 1
            if ContainsSpecialWords(lines):
                print("at row number: " + str(lineNumber) + " " + lines)
        fin.close()

你可能感兴趣的:(Python 遍历文件,字符串操作)