'''
Author: your name
Date: 2024-02-04 21:58:33
LastEditTime: 2024-02-04 22:06:08
LastEditors: haihangyu-sz
Description: In User Settings Edit
FilePath: \Crawler\he.py
'''
import os
import re
# 获å–当å‰è„šæœ¬æ‰€åœ¨ç›®å½•
script_directory = os.path.dirname(os.path.realpath(__file__))
chapter_files_directory = os.path.join(script_directory, "chapter_files")
output_file_path = os.path.join(script_directory, "A股教父.txt")
# 定义æå–æ•°å—çš„æ£åˆ™è¡¨è¾¾å¼
number_pattern = re.compile(r'第(\d+)ç« ')
# é历 chapter_files 文件夹下的所有 .txt 文件,并æå–æ•°å—
file_list = []
for filename in os.listdir(chapter_files_directory):
if filename.endswith(".txt"):
match = number_pattern.search(filename)
if match:
# 将匹é…到的数å—转æ¢ä¸ºæ•´æ•°ï¼ŒåŒæ—¶ä¿å˜æ–‡ä»¶å和路径
file_list.append((int(match.group(1)), os.path.join(chapter_files_directory, filename)))
# 按照æå–到的数å—排åºæ–‡ä»¶åˆ—表
file_list.sort(key=lambda x: x[0])
# 将排åºåŽçš„文件内容写入新的文件
with open(output_file_path, 'w', encoding='utf-8') as output_file:
for _, file_path in file_list:
with open(file_path, 'r', encoding='utf-8') as input_file:
content = input_file.read()
output_file.write(f"File: {os.path.basename(file_path)}\n\n{content}\n\n{'='*30}\n")
print("åˆå¹¶å®Œæˆ")