开发者

Use python tarfile add without normpath being applied to arcname

开发者 https://www.devze.com 2023-04-02 06:46 出处:网络
I\'m using python\'s tarfile library to create a gzipped tar file. The tar file needs to have absolute pathnames for reasons that I\'ve got no control over. (I\'m aware that this isn\'t normal practi

I'm using python's tarfile library to create a gzipped tar file.

The tar file needs to have absolute pathnames for reasons that I've got no control over. (I'm aware that this isn't normal practice.)

When I call

t开发者_运维技巧arobject.add("/foo/xx1", "/bar/xx1")

the arcname argument "/bar/xx1" is run through os.path.normpath() and converted to "bar/xx1"

How do I avoid this and end up with "/bar/xx1" as I require?

I've read that I can replace normpath somewhere, but I'm fairly new to Python and I'm not sure how to do this or what the wider implications would be.

edit

After looking at this question I had a closer look at the tarinfo object, and this seems to work:

my_tarinfo = tarobject.gettarinfo("/foo/xx1")
my_tarinfo.name = "/bar/xx1"
tarobject.addfile(my_tarinfo, file("/foo/xx1"))    


You really do strange things. You need to copy /usr/lib64/python/tarfile.py to a directory in your PYTHONPATH. Then you can modify this file. But you need to bundle you own tarfile module with your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号