发布时间:2019-09-23 17:02:02编辑:auto阅读(2088)
1.压缩一个文件夹下的所有文件
#coding=utf8
import os
import tarfile
__author__ = 'Administrator'
def main():
cwd = os.getcwd()
tar = tarfile.open('test.tar','w:gz')
for root ,dir,files in os.walk(cwd):
for file in files:
fullpath = os.path.join(root,file)
tar.add(fullpath)
if __name__=='__main__':
main()
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
import os
import tarfile
def py_files(members):
for tarinfo in members:
if os.path.splitext(tarinfo.name)[1] == ".py":
yield tarinfo
tar = tarfile.open("sample.tar.gz")
tar.extractall(members=py_files(tar))
tar.close()
https://docs.python.org/2/library/tarfile.html
上一篇: cmd python 默认路径修改
下一篇: python小技巧——list利用fin
50170
49440
40088
37120
31528
28362
27317
22106
22071
20406
59°
649°
473°
458°
666°
597°
1196°
2259°
2082°
1620°