如何使用tarfile实现目录过滤的打包文件

如何在不使用python调用shell的情况下,就是不使用subprocess,os.system等调用tar命令情况下,使用python标准库的tarfile实现tar中--exclude的效果,例如:

tar -cvpzf /media/sda7/backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/mnt --exclude=/sys --exclude=/media /

这里对目录/下的/proc/lost+found,/mnt,/sys/media目录进行过滤,并其结果打包在/media/sda7/backup.tgz文件中。
下面的解决方案是可以的,但是对于/home/sky目录下的文件打包就不可以了:

#!/usr/bin/env python

import tarfile

exclude_names = ['build']
def filter_func(tarinfo):
    if tarinfo.name not in exclude_names and tarinfo.isdir():
        return None
    else:
        return tarinfo
t = tarfile.open("backup.tgz", "w:gz")
t.add("/home/sky", filter=filter_func)

比如我在sky目录下有1个build目录,只对其进行打包的话,结果如下:

sky@debian:~$ tar -tvf backup.tgz 

可以发现目录下完全没有文件。

阅读 7.4k
3 个回答

TarFile.add(name, arcname=None, recursive=True, exclude=None, *, filter=None)
Add the file name to the archive. name may be any type of file (directory, fifo, symbolic link, etc.). If given, arcname specifies an alternative name for the file in the archive. Directories are added recursively by default. This can be avoided by setting recursive to False. If exclude is given, it must be a function that takes one filename argument and returns a boolean value. Depending on this value the respective file is either excluded (True) or added (False). If filter is specified it must be a keyword argument. It should be a function that takes a TarInfo object argument and returns the changed TarInfo object. If it instead returns None the TarInfo object will be excluded from the archive. See Examples for an example.

利用filter参数设定filter函数,filter函数接收TarInfo对象,在filter函数内判断tarinfo对象的类型和名字,如果不符合要求就不返回传入的对象。

import tarfile
exclude_names = ['proc', 'lost+found', 'mnt', 'sys', 'media']
def filter_func(tarinfo):
    if tarinfo.name in exclude_names and tarinfo.isdir():
        return None
    else:
        return tarinfo
t = tarfile.open("/media/sda7/backup.tgz", "w:gz")
t.add("/", filter=filter_func)
#!/usr/bin/env python
import tarfile
exclude_names = ['home/sky/build']
def filter_func(tarinfo):
    if tarinfo.name not in exclude_names and tarinfo.isdir():
        return None
    else:
        return tarinfo
t = tarfile.open("backup.tgz", "w:gz")
t.add("/home/sky", filter=filter_func)
t.close()
请输入代码

exclude_names 列表中写成‘home/sky/build’ 才可以针对build打包

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题