修复自然数排序问题

This commit is contained in:
岛风 2024-02-20 15:34:08 +08:00
parent be1bf3cb4f
commit 84546c76fd
1 changed files with 6 additions and 1 deletions

View File

@ -1,6 +1,11 @@
import os
import urllib.parse
import argparse
import re
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_names, base_url, sub_directory):
"""根据文件名、基础URL和子目录生成URL链接"""
@ -10,7 +15,7 @@ def generate_urls(file_names, base_url, sub_directory):
base_url += '/'
if not sub_directory.endswith('/'):
sub_directory += '/'
for name in file_names:
for name in sorted(file_names, key=natural_sort_key): # 使用自定义排序
# 对文件名进行URL编码
encoded_name = urllib.parse.quote(name)
urls.append(f"{base_url}{sub_directory}{encoded_name}")