-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathftpwalk.py
35 lines (31 loc) · 1.11 KB
/
ftpwalk.py
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
import os
class ftp_walker(object):
def __init__(self, connection, root):
self.connection = connection
self.root = root
def listdir(self, _path):
file_list, dirs, nondirs = [], [], []
try:
self.connection.cwd(_path)
except Exception as exp:
print("the current path is : ", self.connection.pwd(), exp.__str__(), _path)
return [], []
else:
self.connection.retrlines('LIST', lambda x: file_list.append(x.split()))
for info in file_list:
ls_type, name = info[0], info[-1]
if ls_type.startswith('d'):
dirs.append(name)
else:
nondirs.append(name)
return dirs, nondirs
def Walk(self, top, path=''):
dirs, nondirs = self.listdir(top)
yield (path or top), dirs, nondirs
path = top
for name in dirs:
path = os.path.join(path, name)
for x in self.Walk(name, path):
yield x
self.connection.cwd('..')
path = os.path.dirname(path)