| import os
|
|
|
| def get_all_files(directory, exclude=None):
|
| if exclude is None:
|
| exclude = {'.git', '.gitattributes'}
|
|
|
| file_list = []
|
| for root, dirs, files in os.walk(directory, topdown=True):
|
|
|
| dirs[:] = [d for d in dirs if d not in exclude]
|
|
|
| for file in files:
|
| if file not in exclude:
|
| file_list.append(os.path.join(root, file))
|
| return file_list
|
|
|
| def write_file_contents(file_list, output_file):
|
| with open(output_file, 'w', encoding='utf-8') as out:
|
| for file_path in file_list:
|
| out.write(f"文件名:{file_path}\n")
|
| out.write("文件内容:\n")
|
| try:
|
| with open(file_path, 'r', encoding='utf-8') as f:
|
| content = f.read()
|
| out.write(content)
|
| except Exception as e:
|
| out.write(f"无法读取文件内容: {str(e)}\n")
|
| out.write("\n\n" + "-"*50 + "\n\n")
|
|
|
| if __name__ == "__main__":
|
| current_directory = os.getcwd()
|
| exclude_list = {'.git', '.gitattributes'}
|
| file_list = get_all_files(current_directory, exclude_list)
|
| write_file_contents(file_list, 'output.txt')
|
| print("文件内容已输出到 output.txt") |