使用Python的tarfile创建tarball时保留文件权限 [英] preserving file permission when creating a tarball with Python's tarfile
问题描述
你好stackoverflowers
hello stackoverflowers,
使用Python的tarfile模块时,我想保留原始文件权限. 一旦压缩包被解压缩,我就会丢失很多可执行文件的权限.
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.
我正在做这样的事情:
import tarfile
tar = tarfile.open("mytarball.tar.gz", 'w:gz')
tar.add('my_folder') #tar the entire folder
tar.close()
然后我使用shutil将其从Windows复制到linux计算机(映射有samba):
Then I copy it from windows to a linux machine (mapped with samba) using shutil:
shutil.copy("mytarball.tar.gz",unix_dir)
然后,要在Linux中提取压缩包
Then, to extract the tarball in linux I do
unix>tar -xvf mytarball.tar.gz
解压缩tarball后,我会丢失文件的所有'x'权限
After the tarball is extracted I lose all the 'x' permissions on my files
有什么线索可以解决这个问题吗?
Any clues how to solve this issue?
致谢
推荐答案
如果您知道哪些文件应具有执行权限,则可以使用过滤器功能手动设置权限:
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)
这篇关于使用Python的tarfile创建tarball时保留文件权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!