deflist_all_files(rootdir): files = [] # 列出文件夹下所有的目录与文件 list = os.listdir(rootdir) for i inrange(0, len(list)): path = os.path.join(rootdir, list[i]) if os.path.isdir(path): files.extend(list_all_files(path)) if os.path.isfile(path): files.append(path) return files
# 项目目录 path = r"D:\wamp64\www\project" files = list_all_files(path) regex = r"(['])([^']*[\u4e00-\u9fa5]+.*?)\1|([\"])([^\"]*[\u4e00-\u9fa5]+.*?)\1" for file in files: f = open(file, "r", encoding="utf8") for line in f.readlines(): matches = re.findall(regex, line) for match in matches: print(match[1]) f.close()