Python监听读取的文件

import os

def read_hook(file_path):
    print("Reading file:", file_path)

    # 在这里进行钩子操作,例如记录读取的文件路径

    # 返回文件内容
    with open(file_path, "rb") as file:
        content = file.read()
    return content

# 要监听的目录路径
directory_path = "/path/to/directory"

# 用于记录读取的文件列表
read_files = []

# 递归遍历目录中的文件
for root, dirs, files in os.walk(directory_path):
    for filename in files:
        file_path = os.path.join(root, filename)

        # 使用钩子函数读取文件
        file_content = read_hook(file_path)

        # 将读取的文件路径添加到列表中
        read_files.append(file_path)

# 打印读取的文件列表
print("Read files:", read_files)

 

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享