这是适合我的Python代码。评论是西班牙语,但该应用程序易于理解
from ftplib import FTP
import sys
def imprimirMensaje():
print "------------------------------------------------------"
print "-- COMMAND LINE EXAMPLE --"
print "------------------------------------------------------"
print ""
print ">>> Cliente FTP en Python "
print ""
print ">>> python <appname>.py <host> <port> <user> <pass> "
print "------------------------------------------------------"
def f(s):
print s
def download(j):
print "Descargando=>",files[j]
fhandle = open(files[j], 'wb')
ftp.retrbinary('RETR ' + files[j], fhandle.write)
fhandle.close()
ip = sys.argv[1]
puerto = sys.argv[2]
usuario = sys.argv[3]
password = sys.argv[4]
ftp = FTP(ip)
ftp.login(usuario,password)
files = ftp.nlst()
for i,v in enumerate(files,1):
print i,"->",v
print ""
i = int(raw_input("Pon un Nº para descargar el archivo or pulsa 0 para descargarlos\n"))
if i==0:
for j in range(len(files)):
download(j)
if i>0 and i<=len(files):
download(i-1)
with
此处来完成时关闭文件句柄:with open(filename, "wb") as file: ftp.retrbinary("RETR " + filename, file.write)