-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownload_from_FTP.py
More file actions
71 lines (59 loc) · 1.8 KB
/
Download_from_FTP.py
File metadata and controls
71 lines (59 loc) · 1.8 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
import sys, ftplib, os, time, pathlib
server = "prism.oregonstate.edu"
source = "/daily/vpdmin/"
destination = "S:\\GCMC\\Data\\Climate\\PRISM/vpdmin/daily_4km/"
interval = 0.05
ftp = ftplib.FTP(server)
ftp.login()
ftp.cwd(source)
varlist = ftp.nlst()
print(varlist)
def downloadFiles(path, destination):
try:
ftp.cwd(path)
os.chdir(destination)
mkdir_p(destination[0 : len(destination) - 1] + "/" + str.split(path, "/")[-2])
print(
"Created: "
+ destination[0 : len(destination) - 1]
+ "/"
+ str.split(path, "/")[-2]
)
except OSError:
pass
except ftplib.error_perm:
print("Error: could not change FTP path to " + path)
sys.exit("Ending Application")
filelist = ftp.nlst()
for file in filelist:
time.sleep(interval)
try:
ftp.cwd(path + file + "/")
downloadFiles(path + file + "/", destination)
except ftplib.error_perm:
os.chdir(
destination[0 : len(destination) - 1] + "/" + str.split(path, "/")[-2]
)
try:
ftp.retrbinary(
"RETR " + file,
open(
os.path.join(
destination + "/" + str.split(path, "/")[-2], file
),
"wb",
).write,
)
print("Downloaded: " + file)
except:
print("Error: File could not be downloaded " + file)
return
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
downloadFiles(source, destination)