python script:
find_path.py
import os
def search_and_save(root_folder, search_string, output_file):
with open(output_file, 'w') as output:
output.write("{:<50} {:<10} {}\n".format("File Path", "Line Number", "Line Content"))
output.write("="*80 + "\n")
for foldername, subfolders, filenames in os.walk(root_folder):
for filename in filenames:
file_path = os.path.join(foldername, filename)
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line_num, line in enumerate(file, start=1):
if search_string in line:
formatted_line = "{:<50} {:<10} {}".format(file_path, line_num, line.strip())
print(formatted_line)
output.write(formatted_line + "\n")
except Exception as e:
print(f"Error reading file {file_path}: {e}")
# 设置根文件夹路径,搜索的字符串,和输出文件名
root_folder = "./"
search_string = "Hier_Level.rpt"
output_file = "dir_path.list"
# 调用函数进行搜索并保存结果
search_and_save(root_folder, search_string, output_file)
例子:
把该脚本copy到根目录中,然后把search_string = "Hier_Level.rpt" 修改成想要查找的内容,
可以是任何字符串。
执行python find_path.py
命令行打印的结果:
dir_path.list 中的结果: