源文件名:SrcFile.txt

源文件格式:
text
「美女动态壁纸视频合集」
https://pan.quark.cn/s/0b3c2165f724

「2025高中英语学习资料包 (知识点+教辅+试卷)」
https://pan.quark.cn/s/5c0871d34817
        

支持:序号索引,安全创建,q键跳过,e键退出,兼容换行,删旧存新

python
          # -*- coding: utf-8 -*-
          import os
          import sys
          
          # 设置默认编码为 UTF-8
          reload(sys)
          sys.setdefaultencoding('utf-8')
          
          try:
              # Python 3
              from io import open
          except ImportError:
              # Python 2
              pass
          
          # 定义颜色代码
          class Colors:
              HEADER = '\033[95m'
              OKBLUE = '\033[94m'
              OKGREEN = '\033[92m'
              WARNING = '\033[93m'
              FAIL = '\033[91m'
              ENDC = '\033[0m'
              BOLD = '\033[1m'
              UNDERLINE = '\033[4m'
          
          
          def read_links(file_path):
              links = []
              try:
                  with open(file_path, 'r', encoding='utf-8') as file:
                      lines = file.readlines()
                      i = 0
                      while i < len(lines):
                          if lines[i].strip():
                              name = lines[i].strip()
                              if i + 1 < len(lines):
                                  link = lines[i + 1].strip()
                                  links.append((name, link))
                                  print("读取名字 {} url: {}".format(name, link))
                                  i += 2
                          else:
                              i += 1
              except Exception as e:
                  print("读取文件 {} 时出错: {}".format(file_path, e))
              return links
          
          
          def update_source_file(remaining_links, base_file):
              try:
                  with open(base_file, 'w', encoding='utf-8') as file:
                      for name, link in remaining_links:
                          str = "{}\n{}\n\n".format(name, link)
                          file.write(unicode(str, 'utf-8'))
              except Exception as e:
                  print("更新文件 {} 时出错: {}".format(base_file, e))
          
          
          def classify_links(links, base_file):
              remaining_links = links[:]
              string_array = ["1_教育", "2_教程", "3_少儿", "4_电商", "5_健康", "6_娱乐"]
              for index, (name, link) in enumerate(links):
                  print(Colors.OKBLUE + "名称: {}".format(name) + Colors.ENDC)
                  print(Colors.OKGREEN + "链接: {}".format(link) + Colors.ENDC)
                  category_input = raw_input("请输入分类文件名(如 1_教育 2_教程 3_少儿 4_电商 5_健康 6_娱乐 等,输入 q 跳过,输入 e 退出): ")
                  if category_input.lower() == 'q':
                      continue
                  elif category_input.lower() == 'e':
                      print("用户选择退出程序。")
                      update_source_file(remaining_links, base_file)
                      return remaining_links
                  else:
                      category = None
                      try:
                          result = next((string for string in string_array if string.startswith(category_input)), None)
                          if result:
                              category = "{}.txt".format(result)
                              print("提取 {} ".format(category))
                              with open(category, 'a', encoding='utf-8') as file:
                                  content = "{}\n{}\n\n".format(name, link)
                                  file.write(unicode(content, 'utf-8'))
                              # 从剩余链接中移除已处理的链接
                              remaining_links.pop(index - (len(links) - len(remaining_links)))
                          else:
                              print("未找到匹配的分类,请重新输入。")
                      except Exception as e:
                          print("写入 {} 时出错: {}".format(category, e))
                  # 每处理一次数据,更新源文件
                  update_source_file(remaining_links, base_file)
          
              return remaining_links
          
          
          if __name__ == "__main__":
              base_file = 'SrcFile.txt'
              if not os.path.exists(base_file):
                  print("{} 文件不存在。".format(base_file))
              else:
                  links = read_links(base_file)
                  remaining_links = classify_links(links, base_file)
        
比如“动态壁纸”属于‘娱乐’类型,那么输入序号‘6’,就会新建一个‘6_娱乐.txt’,同时动态壁纸这条分享链接就迁移过去了