-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathftp.py
52 lines (44 loc) · 1.5 KB
/
ftp.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# TODO: make an upload function and lean up the codeom
import ftplib
from ftplib import FTP
import os
# connecting to the server
try:
ftp = FTP("{ftp}")
ftp.login(user="{name}", passwd="{password}")
print(ftp.getwelcome()) # welcome messages
wdir = ftp.pwd()
print(wdir)
cwd = input("Enter the directory you want to work in -> ")
ftp.cwd(cwd)
files = []
ftp.dir(files.append)
for file in files:
print(file) # print the files in the working directory
if_upload = input("Do you want to download a file? (y/N) : ")
if if_upload == 'y':
file_copy = input("Enter file to download -> ")
def changeDir():
os.chdir(cwd[12:])
print("Changed directory to ", cwd[12:], " ...")
if not os.path.isfile(file_copy):
print("Creating file ...")
ftp.retrbinary("RETR " + file_copy, open(file_copy, 'wb').write)
print("File downloaded successfully.")
else:
print("File already exists.")
def getFile(ftp, file_copy):
try:
if len(cwd) > 11:
if not os.path.exists(cwd[12:]):
os.makedirs(cwd[12:])
print("Created directory ", cwd[12:], " ...")
changeDir()
else:
print("Path already exists.")
changeDir()
except ftplib.all_errors as e:
print(e)
getFile(ftp, file_copy)
except ftplib.all_errors as e:
print('FTP error:', e)