In [20]:
import os.path as path
from subprocess import call
import glob
import magic
In [63]:
m= magic.Magic()
def walkf(arg, dirname, files):
    for fn in files:
        if path.isfile(dirname+"/"+fn):
            flist.append(dirname+"/"+fn)
for i in range(150,-1, -1):    
    print "i=", i
    fl = filter(path.isdir, glob.glob("*"))
    if len(fl)>1:
        print "too many dirs"
        break    
    fd = fl[0]
    print "fd=", fd    
    call(["mv", fd, "tmp"]) 
    flist = []
    path.walk("tmp", walkf, None)
    print flist
    if len(flist)>1:
        print "too many files"
    f= flist[0]
    print f
    call(["mv", f, "x"])
    call(["rm", "-fr", "tmp"])
    fm = m.id_filename("x")
    print "magic", fm
    call(["7z", "x", "x"])
    if path.exists("x~"):
        if "tar" in m.id_filename("x~"):
            call(["tar", "xf", "x~"])
            call(['rm', "x~"])
        else:
            "????"
            break                
m.close()  
i= 150
fd= 137
['tmp/137.zip']
tmp/137.zip
magic Zip archive data, at least v1.0 to extract
i= 149
fd= 136
['tmp/136.tar.gz']
tmp/136.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:10 2014
i= 148
fd= 135
['tmp/135.tar.bz2']
tmp/135.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 147
fd= 134
['tmp/134.tar.gz']
tmp/134.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:10 2014
i= 146
fd= 133
['tmp/133.zip']
tmp/133.zip
magic Zip archive data, at least v1.0 to extract
i= 145
fd= 132
['tmp/132.tar.bz2']
tmp/132.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 144
fd= 131
['tmp/131.zip']
tmp/131.zip
magic Zip archive data, at least v1.0 to extract
i= 143
fd= 130
['tmp/130.tar.gz']
tmp/130.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:10 2014
i= 142
fd= 129
['tmp/129.tar.bz2']
tmp/129.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 141
fd= 128
['tmp/128.zip']
tmp/128.zip
magic Zip archive data, at least v1.0 to extract
i= 140
fd= 127
['tmp/127.tar.gz']
tmp/127.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:10 2014
i= 139
fd= 126
['tmp/126.tar.bz2']
tmp/126.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 138
fd= 125
['tmp/125.tar.bz2']
tmp/125.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 137
fd= 124
['tmp/124.zip']
tmp/124.zip
magic Zip archive data, at least v1.0 to extract
i= 136
fd= 123
['tmp/123.tar.gz']
tmp/123.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:10 2014
i= 135
fd= 122
['tmp/122.zip']
tmp/122.zip
magic Zip archive data, at least v1.0 to extract
i= 134
fd= 121
['tmp/121.zip']
tmp/121.zip
magic Zip archive data, at least v1.0 to extract
i= 133
fd= 120
['tmp/120.tar.gz']
tmp/120.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:10 2014
i= 132
fd= 119
['tmp/119.tar.bz2']
tmp/119.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 131
fd= 118
['tmp/118.tar.bz2']
tmp/118.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 130
fd= 117
['tmp/117.tar.bz2']
tmp/117.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 129
fd= 116
['tmp/116.zip']
tmp/116.zip
magic Zip archive data, at least v1.0 to extract
i= 128
fd= 115
['tmp/115.zip']
tmp/115.zip
magic Zip archive data, at least v1.0 to extract
i= 127
fd= 114
['tmp/114.tar.bz2']
tmp/114.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 126
fd= 113
['tmp/113.tar.bz2']
tmp/113.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 125
fd= 112
['tmp/112.tar.gz']
tmp/112.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 124
fd= 111
['tmp/111.tar.gz']
tmp/111.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 123
fd= 110
['tmp/110.zip']
tmp/110.zip
magic Zip archive data, at least v1.0 to extract
i= 122
fd= 109
['tmp/109.tar.bz2']
tmp/109.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 121
fd= 108
['tmp/108.tar.bz2']
tmp/108.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 120
fd= 107
['tmp/107.tar.gz']
tmp/107.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 119
fd= 106
['tmp/106.tar.gz']
tmp/106.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 118
fd= 105
['tmp/105.zip']
tmp/105.zip
magic Zip archive data, at least v1.0 to extract
i= 117
fd= 104
['tmp/104.zip']
tmp/104.zip
magic Zip archive data, at least v1.0 to extract
i= 116
fd= 103
['tmp/103.tar.gz']
tmp/103.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 115
fd= 102
['tmp/102.tar.bz2']
tmp/102.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 114
fd= 101
['tmp/101.tar.bz2']
tmp/101.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 113
fd= 100
['tmp/100.tar.gz']
tmp/100.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 112
fd= 99
['tmp/99.zip']
tmp/99.zip
magic Zip archive data, at least v1.0 to extract
i= 111
fd= 98
['tmp/98.tar.bz2']
tmp/98.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 110
fd= 97
['tmp/97.zip']
tmp/97.zip
magic Zip archive data, at least v1.0 to extract
i= 109
fd= 96
['tmp/96.tar.bz2']
tmp/96.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 108
fd= 95
['tmp/95.tar.gz']
tmp/95.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 107
fd= 94
['tmp/94.tar.bz2']
tmp/94.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 106
fd= 93
['tmp/93.zip']
tmp/93.zip
magic Zip archive data, at least v1.0 to extract
i= 105
fd= 92
['tmp/92.tar.gz']
tmp/92.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 104
fd= 91
['tmp/91.zip']
tmp/91.zip
magic Zip archive data, at least v1.0 to extract
i= 103
fd= 90
['tmp/90.zip']
tmp/90.zip
magic Zip archive data, at least v1.0 to extract
i= 102
fd= 89
['tmp/89.tar.gz']
tmp/89.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 101
fd= 88
['tmp/88.tar.gz']
tmp/88.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 100
fd= 87
['tmp/87.zip']
tmp/87.zip
magic Zip archive data, at least v1.0 to extract
i= 99
fd= 86
['tmp/86.tar.gz']
tmp/86.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 98
fd= 85
['tmp/85.tar.bz2']
tmp/85.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 97
fd= 84
['tmp/84.tar.bz2']
tmp/84.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 96
fd= 83
['tmp/83.tar.gz']
tmp/83.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 95
fd= 82
['tmp/82.zip']
tmp/82.zip
magic Zip archive data, at least v1.0 to extract
i= 94
fd= 81
['tmp/81.zip']
tmp/81.zip
magic Zip archive data, at least v1.0 to extract
i= 93
fd= 80
['tmp/80.zip']
tmp/80.zip
magic Zip archive data, at least v1.0 to extract
i= 92
fd= 79
['tmp/79.zip']
tmp/79.zip
magic Zip archive data, at least v1.0 to extract
i= 91
fd= 78
['tmp/78.tar.bz2']
tmp/78.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 90
fd= 77
['tmp/77.zip']
tmp/77.zip
magic Zip archive data, at least v1.0 to extract
i= 89
fd= 76
['tmp/76.tar.bz2']
tmp/76.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 88
fd= 75
['tmp/75.zip']
tmp/75.zip
magic Zip archive data, at least v1.0 to extract
i= 87
fd= 74
['tmp/74.zip']
tmp/74.zip
magic Zip archive data, at least v1.0 to extract
i= 86
fd= 73
['tmp/73.zip']
tmp/73.zip
magic Zip archive data, at least v1.0 to extract
i= 85
fd= 72
['tmp/72.zip']
tmp/72.zip
magic Zip archive data, at least v1.0 to extract
i= 84
fd= 71
['tmp/71.tar.gz']
tmp/71.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 83
fd= 70
['tmp/70.tar.bz2']
tmp/70.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 82
fd= 69
['tmp/69.tar.gz']
tmp/69.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 81
fd= 68
['tmp/68.tar.bz2']
tmp/68.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 80
fd= 67
['tmp/67.zip']
tmp/67.zip
magic Zip archive data, at least v1.0 to extract
i= 79
fd= 66
['tmp/66.zip']
tmp/66.zip
magic Zip archive data, at least v1.0 to extract
i= 78
fd= 65
['tmp/65.tar.bz2']
tmp/65.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 77
fd= 64
['tmp/64.zip']
tmp/64.zip
magic Zip archive data, at least v1.0 to extract
i= 76
fd= 63
['tmp/63.tar.bz2']
tmp/63.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 75
fd= 62
['tmp/62.tar.bz2']
tmp/62.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 74
fd= 61
['tmp/61.tar.gz']
tmp/61.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 73
fd= 60
['tmp/60.zip']
tmp/60.zip
magic Zip archive data, at least v1.0 to extract
i= 72
fd= 59
['tmp/59.zip']
tmp/59.zip
magic Zip archive data, at least v1.0 to extract
i= 71
fd= 58
['tmp/58.tar.bz2']
tmp/58.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 70
fd= 57
['tmp/57.tar.bz2']
tmp/57.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 69
fd= 56
['tmp/56.tar.bz2']
tmp/56.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 68
fd= 55
['tmp/55.zip']
tmp/55.zip
magic Zip archive data, at least v1.0 to extract
i= 67
fd= 54
['tmp/54.zip']
tmp/54.zip
magic Zip archive data, at least v1.0 to extract
i= 66
fd= 53
['tmp/53.tar.bz2']
tmp/53.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 65
fd= 52
['tmp/52.tar.bz2']
tmp/52.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 64
fd= 51
['tmp/51.zip']
tmp/51.zip
magic Zip archive data, at least v1.0 to extract
i= 63
fd= 50
['tmp/50.tar.gz']
tmp/50.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 62
fd= 49
['tmp/49.zip']
tmp/49.zip
magic Zip archive data, at least v1.0 to extract
i= 61
fd= 48
['tmp/48.tar.bz2']
tmp/48.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 60
fd= 47
['tmp/47.tar.gz']
tmp/47.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:09 2014
i= 59
fd= 46
['tmp/46.zip']
tmp/46.zip
magic Zip archive data, at least v1.0 to extract
i= 58
fd= 45
['tmp/45.tar.bz2']
tmp/45.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 57
fd= 44
['tmp/44.zip']
tmp/44.zip
magic Zip archive data, at least v1.0 to extract
i= 56
fd= 43
['tmp/43.tar.bz2']
tmp/43.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 55
fd= 42
['tmp/42.tar.bz2']
tmp/42.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 54
fd= 41
['tmp/41.tar.gz']
tmp/41.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 53
fd= 40
['tmp/40.zip']
tmp/40.zip
magic Zip archive data, at least v1.0 to extract
i= 52
fd= 39
['tmp/39.zip']
tmp/39.zip
magic Zip archive data, at least v1.0 to extract
i= 51
fd= 38
['tmp/38.tar.bz2']
tmp/38.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 50
fd= 37
['tmp/37.zip']
tmp/37.zip
magic Zip archive data, at least v1.0 to extract
i= 49
fd= 36
['tmp/36.tar.gz']
tmp/36.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 48
fd= 35
['tmp/35.tar.gz']
tmp/35.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 47
fd= 34
['tmp/34.tar.bz2']
tmp/34.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 46
fd= 33
['tmp/33.tar.bz2']
tmp/33.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 45
fd= 32
['tmp/32.tar.gz']
tmp/32.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 44
fd= 31
['tmp/31.tar.bz2']
tmp/31.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 43
fd= 30
['tmp/30.tar.bz2']
tmp/30.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 42
fd= 29
['tmp/29.tar.gz']
tmp/29.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 41
fd= 28
['tmp/28.tar.gz']
tmp/28.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 40
fd= 27
['tmp/27.zip']
tmp/27.zip
magic Zip archive data, at least v1.0 to extract
i= 39
fd= 26
['tmp/26.zip']
tmp/26.zip
magic Zip archive data, at least v1.0 to extract
i= 38
fd= 25
['tmp/25.tar.bz2']
tmp/25.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 37
fd= 24
['tmp/24.zip']
tmp/24.zip
magic Zip archive data, at least v1.0 to extract
i= 36
fd= 23
['tmp/23.tar.gz']
tmp/23.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 35
fd= 22
['tmp/22.tar.gz']
tmp/22.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 34
fd= 21
['tmp/21.zip']
tmp/21.zip
magic Zip archive data, at least v1.0 to extract
i= 33
fd= 20
['tmp/20.tar.gz']
tmp/20.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 32
fd= 19
['tmp/19.tar.bz2']
tmp/19.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 31
fd= 18
['tmp/18.tar.gz']
tmp/18.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 30
fd= 17
['tmp/17.tar.gz']
tmp/17.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 29
fd= 16
['tmp/16.tar.bz2']
tmp/16.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 28
fd= 15
['tmp/15.tar.bz2']
tmp/15.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 27
fd= 14
['tmp/14.tar.gz']
tmp/14.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 26
fd= 13
['tmp/13.zip']
tmp/13.zip
magic Zip archive data, at least v1.0 to extract
i= 25
fd= 12
['tmp/12.tar.bz2']
tmp/12.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 24
fd= 11
['tmp/11.zip']
tmp/11.zip
magic Zip archive data, at least v1.0 to extract
i= 23
fd= 10
['tmp/10.zip']
tmp/10.zip
magic Zip archive data, at least v1.0 to extract
i= 22
fd= 9
['tmp/9.tar.gz']
tmp/9.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 21
fd= 8
['tmp/8.zip']
tmp/8.zip
magic Zip archive data, at least v1.0 to extract
i= 20
fd= 7
['tmp/7.tar.bz2']
tmp/7.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 19
fd= 6
['tmp/6.tar.bz2']
tmp/6.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 18
fd= 5
['tmp/5.zip']
tmp/5.zip
magic Zip archive data, at least v1.0 to extract
i= 17
fd= 4
['tmp/4.zip']
tmp/4.zip
magic Zip archive data, at least v1.0 to extract
i= 16
fd= 3
['tmp/3.tar.gz']
tmp/3.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 15
fd= 2
['tmp/2.tar.bz2']
tmp/2.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 14
fd= 1
['tmp/1.tar.gz']
tmp/1.tar.gz
magic gzip compressed data, from Unix, last modified: Fri Aug 15 17:22:08 2014
i= 13
fd= 0
['tmp/0.tar.bz2']
tmp/0.tar.bz2
magic bzip2 compressed data, block size = 900k
i= 12
fd= -1
[]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-63-5395c12c8193> in <module>()
     18     if len(flist)>1:
     19         print "too many files"
---> 20     f= flist[0]
     21     print f
     22     call(["mv", f, "x"])

IndexError: list index out of range
In [58]:
path.walk?
In [47]: