可以添加一个判断来处理文件夹的部分,例如:

if os.path.isdir(file_path): # 处理文件夹,使用tar命令打包并压缩成tar.gz格式 tar_file = os.path.join(output_dir, f'{file_name}.tar.gz') subprocess.run(['tar', '-czf', tar_file, '-C', root, file]) else: # 处理文件,使用pigz进行压缩 subprocess.run(['pigz', '-c', file_path], stdout=open(output_file, 'wb'))

这样就可以使用tar命令将文件夹打包成tar.gz格式了。


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

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