V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
heoolaya
V2EX  ›  问与答

压缩包批量重命名求助

  •  
  •   heoolaya · 2023-03-21 13:39:28 +08:00 · 580 次点击
    这是一个创建于 439 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想用压缩包内的文件夹名称批量重命名压缩包,不想全部解压后再打包,大佬们有没有什么办法
    3 条回复    2023-03-21 23:21:09 +08:00
    coderluan
        1
    coderluan  
       2023-03-21 13:59:43 +08:00
    这种问题其实才是 chatgpt 的正确用途:

    提问“编写 python 程序,获取压缩文件 test.zip 中的文件夹名 name, 要求不进行解压,然后把 test.zip 重命名为 name.zip”

    import zipfile
    import os

    def get_zip_folder_name(file_path):
    with zipfile.ZipFile(file_path, 'r') as zip_file:
    for file_info in zip_file.infolist():
    if file_info.is_dir():
    return os.path.splitext(file_info.filename)[0]
    return None

    def rename_zip_file(file_path, new_file_name):
    os.rename(file_path, new_file_name)

    if __name__ == '__main__':
    zip_file_path = 'test.zip'
    folder_name = get_zip_folder_name(zip_file_path)
    if folder_name:
    new_file_name = folder_name + '.zip'
    rename_zip_file(zip_file_path, new_file_name)
    else:
    print('No folder found in the zip file.')
    heoolaya
        2
    heoolaya  
    OP
       2023-03-21 14:03:29 +08:00
    @coderluan 哈哈哈哈,牛逼
    linuxgo
        3
    linuxgo  
       2023-03-21 23:21:09 +08:00
    windows 下用 totalcmd 可以对文件批量命名,linux 下可以用 krename 或者 krusader
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5030 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:24 · PVG 09:24 · LAX 18:24 · JFK 21:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.