我有以下用于检查版权标记的代码。我目前只检查 userstring1。现在我想检查多个用户字符串。修改以下代码的最佳方法是什么:
- 检查所有正在检查的文件是否有所有用户字符串
打印匹配任何字符串的每个文件的文件名
import os import sys import fnmatch userstring1="Copyright (c) 2012 Company, Inc\nAll Rights Reserved.\nCompany Confidential and Proprietary." userstring2="Copyright (c) 2011-2013 Company, Inc\nAll Rights Reserved.\nCompany Confidential and Proprietary." #print len(sys.argv) #print sys.argv[1] if len(sys.argv) < 2: sys.exit('Usage: check_copyright.py <build directory>') ''' for r,d,f in os.walk(sys.argv[1]): for files in f: userlines = userstring.split('\n') # Separate the string into lines with open(os.path.join(r, files), "r") as file: ''' for path,dirs,files in os.walk(sys.argv[1]): for fname in files: userlines = userstring1.split('\n') # Separate the string into lines # Test the filename for particular pattern matches. for pat in ['*.cpp','*.c','*.h']: if fnmatch.fnmatch(fname,pat): fullname = os.path.join(path,fname) with open(fullname) as file: match = 0 for line in file: if userlines[match] in line: # Check if the line at index `m` is in the user lines match += 1 # Next time check the following line elif match > 0: # If there was no match, reset the counter match = 0 if match >= len(userlines): # If 3 consecutive lines match, then you found a match break if match == len(userlines): # print if you found a match print "MATCH" print match print "LENGTH userlines" print len(userlines) print fullname