我正在迭代文件夹并逐行读取文件以找到一些子字符串。
问题是我想在搜索过程中忽略某些文件夹(例如 bin
或 build
)。
我尝试添加检查 - 但它仍然将其包含在搜索中
def step(ext, dirname, names):
for name in names:
if name.lower().endswith(".xml") or name.lower().endswith(".properties") or name.lower().endswith(".java"):
path = os.path.join(dirname, name)
if not "\bin\"" in path and not "\build" in path:
with open(path, "r") as lines:
print "File: {}".format(path)
i = 0
for line in lines:
m = re.search(r"\{[^:]*:[^\}]*\}", line)
with open("search-result.txt", "a") as output:
if m is not None:
output.write("Path: {0}; \n Line number: {1}; \n String: {2}\n".format(path, i, m.group()))
i+=1
最佳答案
我怀疑您的条件失败,因为 \b
被解释为退格字符,而不是反斜杠字符后跟“b”字符。另外,看起来您在 bin 末尾转义了引号;这是故意的吗?
尝试转义反斜杠。
if not "\\bin\\" in path and not "\\build" in path:
关于python - 检查路径中是否存在某个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30102146/