import os
def process_files():
# 創(chuàng)建目錄
processed_dir = "已處理"
issue_dir = "問(wèn)題文檔"
if not os.path.exists(processed_dir):
os.makedirs(processed_dir)
if not os.path.exists(issue_dir):
os.makedirs(issue_dir)
# 獲取當(dāng)前目錄下的所有txt文件
txt_files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.txt')]
for txt_file in txt_files:
file_path = os.path.abspath(txt_file)
processed_file_path = os.path.join(processed_dir, txt_file)
issue_file_path = os.path.join(issue_dir, txt_file)
# 處理標(biāo)題
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
# 判斷是否有標(biāo)題
found_title = False
for i, line in enumerate(lines):
if line.startswith("標(biāo)題:") or line.startswith(""):
lines[i] = line.split("標(biāo)題:", 1)[-1].split("", 1)[-1]
found_title = True
break
if found_title:
# 寫入處理后的內(nèi)容到新文件
with open(processed_file_path, 'w', encoding='utf-8') as file:
file.writelines(lines)
os.remove(file_path)
print(f"文件 '{txt_file}' 已處理并移動(dòng)到 '已處理' 目錄。")
else:
os.rename(file_path, issue_file_path)
print(f"文件 '{txt_file}' 移動(dòng)到 '內(nèi)容可能有問(wèn)題' 目錄。")
if __name__ == "__main__":
process_files()