modify scripts
This commit is contained in:
61
aabook/tools_other.py
Normal file
61
aabook/tools_other.py
Normal file
@ -0,0 +1,61 @@
|
||||
import os
|
||||
|
||||
def rename_files(list_file, data_dir):
|
||||
"""
|
||||
重命名文件
|
||||
|
||||
Args:
|
||||
list_file: 存放 novel_id 和 novel_name 的文件路径
|
||||
data_dir: 需要重命名文件的目录
|
||||
"""
|
||||
|
||||
# 读取列表文件,构建一个字典,key为novel_name,value为novel_id
|
||||
id_dict = {}
|
||||
with open(list_file, 'r', encoding='utf-8') as f:
|
||||
for line in f:
|
||||
novel_id, novel_name = line.strip().split('\t')
|
||||
id_dict[novel_name] = novel_id
|
||||
|
||||
# 遍历 data 目录下的所有文件
|
||||
for root, dirs, files in os.walk(data_dir):
|
||||
for file in files:
|
||||
if file.endswith('.txt'):
|
||||
# 获取文件名(不含扩展名)
|
||||
novel_name = file[:-4]
|
||||
# 判断文件名是否在字典中
|
||||
if novel_name in id_dict:
|
||||
old_file = os.path.join(root, file)
|
||||
new_file = os.path.join(root, f"{id_dict[novel_name]}_{novel_name}.txt")
|
||||
os.rename(old_file, new_file)
|
||||
print(f"Renamed {old_file} to {new_file}")
|
||||
|
||||
|
||||
def check_and_record(data_dir, search_string, output_file):
|
||||
"""
|
||||
检查文件内容并记录
|
||||
|
||||
Args:
|
||||
data_dir: 需要检查的目录
|
||||
search_string: 需要搜索的字符串
|
||||
output_file: 记录结果的文件
|
||||
"""
|
||||
|
||||
with open(output_file, 'w', encoding='utf-8') as output:
|
||||
for root, dirs, files in os.walk(data_dir):
|
||||
for file in files:
|
||||
if file.endswith('.txt'):
|
||||
novel_name = file[:-4]
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
if search_string in f.read():
|
||||
output.write(novel_name + '\n')
|
||||
print(f"need update: {novel_name}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# rename_files("aabook_down_list.txt", "data")
|
||||
|
||||
data_dir = "data"
|
||||
search_string = "2005-2024 疯情书库"
|
||||
output_file = "aabook_need_update.txt"
|
||||
check_and_record(data_dir, search_string, output_file)
|
||||
Reference in New Issue
Block a user