tarfile — Read and write tar archive files

参考:

https://docs.python.org/2/library/tarfile.html

http://www.jianshu.com/p/bbad16822eab

#解压文件
tarfile.open(filepath, 'r:gz').extractall(inception_pretrain_model_dir)

python中os.walk的用法

如下的文件结构:

 a ->   b   ->   1.txt,  2.txt
             c   ->   3.txt
             d   ->   
           4.txt
           5.txt
for (root, dirs, files) in os.walk('a'):
    #第一次运行时,当前遍历目录为 a
    所以 root == 'a'
         dirs == [ 'b', 'c', 'd']
         files == [ '4.txt', '5.txt']
    
    。。。

    # 接着遍历 dirs 中的每一个目录
    b:  root  = 'a\b'
        dirs  = []
        files = [ '1.txt', '2.txt']
    
    # dirs为空,返回
    # 遍历c
    c:  root  = 'a\c'
        dirs  = []
        files = [ '3.txt' ]
    
    PS : 如果想获取文件的全路径,只需要 
    for f in files:
        path = os.path.join(root,f)
    
    # 遍历d
    d:  root  = 'a\b'
        dirs  = []
        files = []

    遍历完毕,退出循环
原文地址:https://www.cnblogs.com/TMatrix52/p/8017445.html