import tarfile
#压缩文件的方法
tar = tarfile.open('/tmp/demo.tar.gz','w:gz') #gzip压缩
tar.add('/etc/hosts') #添加要压缩的文件
tar.add('/etc/security')
tar.close()
#解压文件的方法
tar = tarfile.open('/tmp/demo.tar.gz','r:gz')
tar.extractall() #解压文件到当前目录
tar.close()
python-tarfile模块(82)
使用tarfile模块进行文件压缩与解压
最新推荐文章于 2025-09-11 14:59:44 发布
本文介绍如何利用Python的tarfile模块来压缩和解压文件,包括使用gzip压缩方法,以及如何将特定文件如'/etc/hosts'和'/etc/security'添加到压缩包中,并从压缩包中解压所有文件到当前目录。
2820

被折叠的 条评论
为什么被折叠?



