将下面python3代码中进度条长度设置为15import osimport subprocessfrom tqdm import tqdmdef pack_filessource_dir output_dir # 检查源目录和输出目录是否存在 if not ospathexistssource_dir or not ospathisdirsource_dir print
将tqdm函数中的参数"bar_format"设置为"{l_bar}{bar:15}{r_bar}",即可将进度条长度设置为15,修改后的代码如下:
import os import subprocess from tqdm import tqdm
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
for root, dirs, files in os.walk(source_dir):
for file in tqdm(files, desc='压缩进度', colour='#00FF00', bar_format="{l_bar}{bar:15}{r_bar}"):
# 获取需要压缩的文件路径
file_path = os.path.join(root, file)
# 拼接输出文件路径
output_file = os.path.join(output_dir, f'{file}.gz')
# 使用pigz进行压缩
subprocess.run(['pigz', '-c', file_path], stdout=open(output_file, 'wb'))
print(f'已将文件{file_path}压缩到{output_file}')
路径
source_dir = '/home/115/up' output_dir = '/home/115/app' pack_files(source_dir, output_dir
原文地址: https://www.cveoy.top/t/topic/c4sM 著作权归作者所有。请勿转载和采集!