本文主要是介绍python3.9 使用tarfile 进行tar文件打包文件含有PaxHeader文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题描述
平台:Win 10 64位
def tar_pak(out_filename, source_dir):if not os.path.exists(source_dir):sys.stdout.write("[tar.err] source dir is null!\n")returnwith tarfile.open(out_filename, "w") as tar:tar.add(source_dir, os.path.basename(source_dir))
使用python 3.7 tarfile tar打包,发现生成的tar包正常。
但是更换了 python3.9后,发现打包后的文件,解压后,有个 @PaxHeader 文件。
查看tar包,发现里面多了一个. 目录,linux下,一般当前目录为【.】, 上级目录为【..】。
python 3.9 打的tar包,里面包含一个【本地磁盘】的【.】目录!!
思路
想移除这个.目录,否则嵌入式软件解压失败。开始网上搜索,发现大家没碰到过此问题!!
查看python tarfile,没发现什么问题。
干脆,我进入tarfile的源文件,查找python3.7 与 python3.9的区别。
终于找到问题点:
python 3.7
DEFAULT_FORMAT = GNU_FORMAT
python 3.9
DEFAULT_FORMAT = PAX_FORMAT
两个版本,打包的默认格式不一样!!
解决方法一:
把: C:\Python39\Lib\tarfile.py 中的:
DEFAULT_FORMAT = PAX_FORMAT 改为:DEFAULT_FORMAT = GNU_FORMAT。
测试发现没问题了。怀疑是windows平台与Linux平台差异引起的,linux平台会多出当前目录【.】与上级目录【..】,tarfile.add 函数递归打包时,把当前目录【.】也打入了tar包。
使用:DEFAULT_FORMAT = GNU_FORMAT,可以解决这个问题。
【不推荐这么改,可以使用第二种方法!!】
解决方法二:
使用tarfile open后,设置格式为: tarfile.GNU_FORMAT
tar.format = tarfile.GNU_FORMAT
def make_tar_package(out_filename, source_dir, tar_type):if not os.path.exists(source_dir):sys.stdout.write("[tar.err] source dir is null!\n" )returnwith tarfile.open(out_filename, tar_type) as tar:tar.format = tarfile.GNU_FORMAT # formattar.add(source_dir, os.path.basename(source_dir))def tar_pak(out_filename, source_dir):if not os.path.exists(source_dir):sys.stdout.write("[tar.err] source dir is null!\n")returnwith tarfile.open(out_filename, "w") as tar:tar.format = tarfile.GNU_FORMAT # formattar.add(source_dir, os.path.basename(source_dir))
tar 打包示例
import os
import sys
import tarfiletar_system_dir_name = "system"
tar_user_dir_name = "user"
tar_default_type = ".tar"def make_tar_package(out_filename, source_dir, tar_type):if not os.path.exists(source_dir):sys.stdout.write("[tar.err] source dir is null!\n" )returnwith tarfile.open(out_filename, tar_type) as tar:tar.format = tarfile.GNU_FORMATtar.add(source_dir, os.path.basename(source_dir))def tar_pak(out_filename, source_dir):if not os.path.exists(source_dir):sys.stdout.write("[tar.err] source dir is null!\n")returnwith tarfile.open(out_filename, "w") as tar:tar.format = tarfile.GNU_FORMATtar.add(source_dir, os.path.basename(source_dir))# 获取上级路径
def get_parent_path():return os.path.dirname(os.getcwd().replace('\\', '/'))# 获取当前路径
def get_current_path():return os.getcwd().replace('\\', '/')# main 函数入口
if __name__ == "__main__":_source_dir = ""_tar_type = ""sys.stdout.write("[tar.file] start!! \n")# 文件夹初始化if len(sys.argv) >= 2:_source_dir = sys.argv[1]sys.stdout.write("[tar] source directory:" + _source_dir + "\n")if len(sys.argv) >= 3:_tar_format = "w:" + sys.argv[2]sys.stdout.write("[tar] tar type:" + _tar_type + "\n")if _source_dir != "":system_src_dir = os.path.join(get_current_path(), (_source_dir + "/" + tar_system_dir_name)).replace('\\', '/')user_src_dir = os.path.join(get_current_path(), (_source_dir + "/" + tar_user_dir_name)).replace('\\', '/')system_tar_filename = os.path.join(get_current_path(), _source_dir + "/"+ tar_system_dir_name + tar_default_type).replace('\\','/')user_tar_filename = os.path.join(get_current_path(), _source_dir + "/"+ tar_user_dir_name + tar_default_type).replace('\\', '/')else:system_src_dir = os.path.join(get_current_path(), tar_system_dir_name).replace('\\', '/')user_src_dir = os.path.join(get_current_path(), tar_user_dir_name).replace('\\', '/')system_tar_filename = os.path.join(get_current_path(), tar_system_dir_name +tar_default_type).replace('\\', '/')user_tar_filename = os.path.join(get_current_path(), tar_user_dir_name +tar_default_type).replace('\\', '/')if os.path.exists(system_src_dir):tar_pak(system_tar_filename, system_src_dir)sys.stdout.write("[tar.file] " + system_src_dir + " ==>" + system_tar_filename + " \n")else:sys.stdout.write("[tar.file] not found dir: " + system_src_dir + " \n")if os.path.exists(user_src_dir):tar_pak(user_tar_filename, user_src_dir)sys.stdout.write("[tar.file] " + user_src_dir + " ==>" + user_tar_filename + " \n")else:sys.stdout.write("[tar.file] not found dir: " + user_src_dir + " \n")sys.stdout.write("[tar.file] end!! \n")
效果
总结
python的问题,可以通过版本的对比,通过查找python API手册,找到更深入的分析方法。
这篇关于python3.9 使用tarfile 进行tar文件打包文件含有PaxHeader文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!