import os import subprocess from alive_progress import alive_bar, config_handler

def pack_files(source_dir, output_dir): # 检查源目录和输出目录是否存在 if not os.path.exists(source_dir) or not os.path.isdir(source_dir): print(f'源目录{source_dir}不存在或不是一个目录') return

if not os.path.exists(output_dir) or not os.path.isdir(output_dir):
    print(f'输出目录{output_dir}不存在或不是一个目录')
    return

# 计算文件总数
total_files = sum([len(files) for root, dirs, files in os.walk(source_dir)])

# 设置进度条样式
config_handler.set_global(length=40, spinner="classic", bar="classic", enrich_print=True)

with alive_bar(total_files, title='压缩进度') as bar:
    for root, dirs, files in os.walk(source_dir):
        if files and not dirs:
            # 处理目录下只有文件的情况
            for file in files:
                # 获取需要压缩的文件路径
                file_path = os.path.join(root, file)

                # 获取文件名和文件后缀
                file_name, file_ext = os.path.splitext(file)

                # 拼接输出文件路径(去掉文件后缀)
                output_file = os.path.join(output_dir, f'{file_name}.gz')

                # 使用pigz进行压缩
                subprocess.run(['pigz', '-c', file_path],
                               stdout=open(output_file, 'wb'))
                # 更新进度条
                bar()
        elif dirs and not files:
            # 处理目录下只有文件夹的情况
            for dir in dirs:
                # 获取需要压缩的文件夹路径
                dir_path = os.path.join(root, dir)

                # 获取文件夹名
                dir_name = os.path.basename(dir_path)

                # 拼接输出文件路径
                output_file = os.path.join(output_dir, f'{dir_name}.tar.gz')

                # 使用tar命令打包并压缩成tar.gz格式
                subprocess.run(['tar', '-czf', output_file, dir_path])

                # 更新进度条
                bar()
        elif files and dirs:
            # 处理目录下既有文件又有文件夹的情况
            for file in files:
                # 获取需要压缩的文件路径
                file_path = os.path.join(root, file)

                # 获取文件名和文件后缀
                file_name, file_ext = os.path.splitext(file)

                # 拼接输出文件路径(去掉文件后缀)
                output_file = os.path.join(output_dir, f'{file_name}.gz')

                # 使用pigz进行压缩
                subprocess.run(['pigz', '-c', file_path],
                               stdout=open(output_file, 'wb'))

                # 更新进度条
                bar()

            for dir in dirs:
                # 获取需要压缩的文件夹路径
                dir_path = os.path.join(root, dir)

                # 获取文件夹名
                dir_name = os.path.basename(dir_path)

                # 拼接输出文件路径
                output_file = os.path.join(output_dir, f'{dir_name}.tar.gz')

                # 使用tar命令打包并压缩成tar.gz格式
                subprocess.run(['tar', '-czf', output_file, dir_path])

                # 更新进度条
                bar()

路径

source_dir = '/home/115/up' output_dir = '/home/115/app' pack_files(source_dir, output_dir

将代码的打包逻辑进行重写当目录下只有文件时将每个文件打包成gz文件当目录下只有文件夹时将每个文件夹打包成targz文件当目录下既有文件又有文件夹时则将每个文件打包成gz将每个文件夹打包成targz文件注意不要递归录下文件夹内的文件注意不要递归录下文件夹内的文件注意不要递归录下文件夹内的文件import osimport subprocessfrom alive_progress import al

原文地址: https://www.cveoy.top/t/topic/deQ3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录