我有以下代码用于检查版权标记。我目前只检查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
答案 0 :(得分:1)
我没有测试过,但我所做的只是将代码放入函数中然后循环每个用户名。
import os
import sys
import fnmatch
if len(sys.argv) < 2:
sys.exit('Usage: check_copyright.py <build directory>')
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."
for path,dirs,files in os.walk(sys.argv[1]):
for fname in files:
for userstring in [userstring1, userstring2]:
test(userstring, path, dirs, fname)
def test(userstring, path, dirs, fname):
userlines = userstring.split('\n')
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