-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFTP_list.py
More file actions
126 lines (71 loc) · 2.27 KB
/
Copy pathFTP_list.py
File metadata and controls
126 lines (71 loc) · 2.27 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import ftplib
import sys
import os
import io
#------------constants------------------
sites=[]
sites.append(['192.168.100.2','nicolas','nicolas'])
selected_site_index=0
site= sites[selected_site_index]
sub_dir= '' # '/sub-directory
file_report= "report.txt"
#-end------------constants---------------
site_url= site[0]
site_user= site[1]
site_pass= site[2]
report= os.path.join(os.path.curdir , file_report)
ftp = ftplib.FTP(site_url)
ftp.login(site_user, site_pass)
def clear_file_report():
if os.path.exists (report):
print("Removing the old report file")
os.remove(report)
else:
print("Creating a new report file")
append("FILE LIST")
def append(text, ):
if os.path.exists (report):
f = open(report, "a")
else:
f = open(report, "w")
f.write ("%s\b"%text)
f.close()
def list_all_ftp_files(ftp, dir):
print("hey")
stream = io.StringIO()
sys.stdout = stream
print("hey")
ftp.nlst(dir)
streamed_result = stream.getvalue()
non_dirs = []
dirs = []
reduced = [x for x in streamed_result.split(' ') if x != '']
reduced = [x.split('\n')[0] for x in reduced]
indexes = [ix + 1 for ix,x in enumerate(reduced) if x == '<DIR>']
folders = [reduced[ix] for ix in indexes]
#files = ftp.mlsd(sub_dir)
if dir == '/':
non_folders = [x for x in ftp.nlst() if x not in folders]
else:
non_folders = [x for x in ftp.nlst(dir) if x not in folders]
non_folders = [dir + '/' + x for x in non_folders]
folders = [dir + '/' + x for x in folders]
if dirs == []:
dirs.extend(folders)
if non_dirs == []:
non_dirs.extend(non_folders)
if len(folders) > 0:
for sub_folder in sorted(folders):
result = list_all_ftp_files(ftp, sub_folder)
dirs.extend(result[0])
non_dirs.extend(result[1])
return non_dirs
def process():
files = []
clear_file_report()
files = list_all_ftp_files(ftp , sub_dir)
for f in files:
print("Writing:%s" % f)
append(f)
if __name__ == "__main__":
process()