recursile files/dirs lister. I need it for writing torrents descriptions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# This Python file uses the following encoding: utf-8
import sys
import os
SPACE = "\t"
def name(dir):
if dir[-1] == "/":
dir = dir[:-1]
n = 1
while (dir[-n] != "/") or (n > len(dir)):
n += 1
if n > len(dir): return dir
return dir[-n+1:]
def rlist(r, lvl=0):
listing = os.listdir(r)
for file in [f for f in listing if not os.path.isdir(r+"/"+f)]:
for i in range(1,lvl):
print SPACE,
print file
for dir in [d for d in listing if os.path.isdir(r+"/"+d)]:
for i in range(1,lvl):
print SPACE,
print "["+dir+"]"
rlist(r+"/"+dir,lvl+1)
def main():
if not os.path.exists(sys.argv[1]) or not os.path.isdir(sys.argv[1]):
print "ERROR: need existing directory as argument"
return 0
print "["+name(sys.argv[1])+"]"
rlist(sys.argv[1],1)
if __name__ == '__main__': main()