我有一个基本的问题(如下#1)和一个问题要问,我不知道对答案(#2)。任何人都可以提供意见如何限制搜索的文件只有特定的扩展
1.How将搜索范围限制到只有特定的extensions
,可以说只有.c
,.h
,.cpp
2.How使点"."
的"\n"
之前在下面可选
userstring="Copyright (c) 2012 Company, Inc.\nAll Rights Reserved.\nCompany Confidential and Proprietary." variable
import os
import sys
import fnmatch
userstring="Copyright (c) 2012 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: python.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
if files.endswith("." + c) or files.endswith("." + cpp):
with open(os.path.join(r, files), "r") as file:
match = 0
for line in file:
if userlines[match] in line.strip('\n\r .'): # 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): # You found a match
print files
usertring
变量
编译错误: -
File "test.py", line 12, in <module>
if files.endswith("." + c) or files.endswith("." + cpp):
NameError: name 'c' is not defined
看看了'glob'模块。 –
不确定您的问题中的代码缩进是否正确。 – Paolo
@AshwiniChaudhary - 根据以下建议使用endswith,但遇到编译错误 – user1927233