preserving file permission when creating a tarball with Python's tarfile

故事扮演 提交于 2019-12-07 21:57:01

问题


hello stackoverflowers,

I want to preserve the original file permissions when using Python's tarfile module. I have quite a few executable files that lose their permissions once the tarball is extracted.

I'm doing something like this:

import tarfile
tar = tarfile.open("mytarball.tar.gz", 'w:gz')
tar.add('my_folder') #tar the entire folder 
tar.close()

Then I copy it from windows to a linux machine (mapped with samba) using shutil:

shutil.copy("mytarball.tar.gz",unix_dir)

Then, to extract the tarball in linux I do

unix>tar -xvf mytarball.tar.gz  

After the tarball is extracted I lose all the 'x' permissions on my files

Any clues how to solve this issue?

Regards


回答1:


If you know which of your files should have execute permissions or not, you can set the permissions manually with a filter function:

def set_permissions(tarinfo):
    tarinfo.mode = 0777 # for example
    return tarinfo

tar.add('my_folder', filter=set_permissions)


来源:https://stackoverflow.com/questions/23389184/preserving-file-permission-when-creating-a-tarball-with-pythons-tarfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!