You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于国家编码表与国家区域文件批量重命名数字编号文件

批量重命名实现方案

前置说明

所有三类文件需放在同一工作目录下,执行脚本前建议先备份所有待重命名文件,避免误操作丢失数据。

方案1:Linux/macOS Shell 脚本

在当前目录下创建rename.sh文件,写入以下内容:

#!/bin/bash
# 加载国家编码映射到关联数组
declare -A country_map
while read -r code name; do
    country_map["$code"]="$name"
done < countryCode.tsv

# 遍历所有符合「数字_数字」格式的待重命名文件
for file in [0-9][0-9]_[0-9]*; do
    # 跳过无效匹配项
    [ -e "$file" ] || continue
    # 拆分国家编码和区域行号
    country_code=${file%%_*}
    line_num=${file##*_}
    # 去除行号前导0避免解析错误
    line_num=$((10#$line_num))
    # 匹配国家名
    country_name=${country_map[$country_code]}
    if [ -z "$country_name" ]; then
        echo "跳过文件 $file:未找到对应国家编码"
        continue
    fi
    # 匹配对应行的区域名,转小写、空格替换为下划线
    region_name=$(sed -n "${line_num}p" "${country_name}.txt" | tr '[:upper:]' '[:lower:]' | tr ' ' '_')
    if [ -z "$region_name" ]; then
        echo "跳过文件 $file:未找到对应行号的区域名"
        continue
    fi
    # 生成新文件名
    new_name="${country_name}_${region_name}"
    # 先预览重命名操作,确认无误后删除echo即可执行实际重命名
    echo mv "$file" "$new_name"
done

执行步骤:

  • 给脚本加执行权限:chmod +x rename.sh
  • 先执行./rename.sh预览所有重命名操作,确认规则无误
  • 删除脚本最后一行的echo,再执行即可完成重命名

方案2:跨平台 Python 实现

适用于Windows、Linux、macOS全环境,创建rename.py写入以下内容:

import os

def main():
    # 加载国家编码映射
    country_map = {}
    with open("countryCode.tsv", "r", encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            code, name = line.split(maxsplit=1)
            country_map[code] = name
    
    # 遍历当前目录所有文件
    for filename in os.listdir("."):
        # 仅处理符合「数字_数字」格式的待重命名文件
        if "_" not in filename:
            continue
        try:
            country_code, line_str = filename.split("_", maxsplit=1)
            line_num = int(line_str)
            if not country_code.isdigit():
                continue
        except ValueError:
            continue
        
        # 校验国家编码合法性
        if country_code not in country_map:
            print(f"跳过文件 {filename}:未找到对应国家编码")
            continue
        country_name = country_map[country_code]
        
        # 读取国家区域文件匹配行号
        region_file = f"{country_name}.txt"
        if not os.path.exists(region_file):
            print(f"跳过文件 {filename}:缺失国家区域文件 {region_file}")
            continue
        
        with open(region_file, "r", encoding="utf-8") as f:
            lines = [l.strip() for l in f if l.strip()]
        if line_num < 1 or line_num > len(lines):
            print(f"跳过文件 {filename}:行号超出区域文件范围")
            continue
        
        # 格式化区域名
        region_name = lines[line_num-1].lower().replace(" ", "_")
        new_name = f"{country_name}_{region_name}"
        
        # 先预览操作,确认无误后注释print行、放开os.rename注释即可执行
        print(f"重命名:{filename} -> {new_name}")
        # os.rename(filename, new_name)

if __name__ == "__main__":
    main()

执行步骤:

  • 先运行python rename.py预览所有重命名操作
  • 确认规则无误后,注释掉print行,放开os.rename的注释,再次运行即可完成重命名

注意事项

  • 区域名包含特殊字符(斜杠、问号等)时可自行在格式化部分增加替换逻辑,避免生成非法文件名
  • 重名文件会被默认覆盖,执行实际重命名前务必做好备份
  • 行号默认从1开始计数,完全匹配需求规则

内容的提问来源于stack exchange,提问作者gesih38335enpaypal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 11:48:04