From 112ab8e56ecde65dc5afdcfe01b64ca303c1258f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=9B=E9=A3=8E?= Date: Thu, 27 Jun 2024 00:58:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=92=E5=BD=92=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- urls/main.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/urls/main.py b/urls/main.py index ccc6159..9dfaea1 100644 --- a/urls/main.py +++ b/urls/main.py @@ -5,12 +5,12 @@ import re import time def natural_sort_key(s): - """为了自然排序的辅助函数,将字符串中的数字部分转换成整数""" + """为了自然排序的辅助函数,将字符串中的数字部分转换成整数""" return [int(text) if text.isdigit() else text.lower() for text in re.split('([0-9]+)', s)] def generate_urls(file_paths, base_url, sub_directory, min_size): """根据文件路径、基础URL、子目录和最小文件大小生成URL链接""" - urls = [] + urls = {} if not base_url.endswith('/'): base_url += '/' if sub_directory and not sub_directory.endswith('/'): @@ -23,14 +23,23 @@ def generate_urls(file_paths, base_url, sub_directory, min_size): relative_path = os.path.relpath(path, start='.') encoded_path = urllib.parse.quote(relative_path) url = f"{base_url}{sub_directory}{encoded_path}" - urls.append(f"{file_size_bytes}:{current_timestamp}:{url}") + dir_name = os.path.dirname(relative_path) + if dir_name not in urls: + urls[dir_name] = [] + urls[dir_name].append(f"{file_size_bytes}:{current_timestamp}:{url}") return urls def save_urls(urls, output_file): """将URL链接保存到文本文件中""" with open(output_file, 'w', encoding='utf-8') as f: - for url in urls: - f.write(url + '\n') + for dir_name, files in urls.items(): + if dir_name == '.': + for file in files: + f.write(f"{file}\n") + else: + f.write(f"{dir_name}:\n") + for file in files: + f.write(f" {file}\n") def parse_arguments(): """解析命令行参数""" @@ -42,7 +51,7 @@ def parse_arguments(): return parser.parse_args() def list_files_recursive(start_path='.', exclude_files=None): - """递归列出目录及其子目录中的所有文件,排除指定的文件""" + """递归列出目录及其子目录中的所有文件,排除指定的文件""" if exclude_files is None: exclude_files = set() file_paths = []