下面的脚本将仅下载ftp站点的文件名而不是我要下载的实际文件,这就是下载后文件大小保持为0的原因。您是否知道导致问题的原因是什么?任何帮助将不胜感激。
import os
from ftplib import FTP
import time
# Connects to remote ftp server
def connect_ftp(hostname, username, password, source_directory, destination_directory):
os.chdir(destination_directory)
print("Connecting to " + hostname + "...")
ftp = FTP(hostname)
ftp.login(username, password)
print(ftp.getwelcome())
print("Connected Successfully!\n")
ftp.cwd(source_directory)
download_files(ftp)
# Download files
def download_files(ftp):
print("This script will only download files, not directories.")
print("Files at " + source_directory + ":\n")
ftp.retrlines("LIST")
file_list = []
ftp.retrlines("NLST", file_list.append)
print("")
i = 0
for filename in file_list:
if (filename != '.') and (filename != '..'):
print("Downloading " + filename + "...")
try:
ftp.retrbinary("RETR " + filename, open(filename, "wb").write)
i=i+1
except Exception as directory_error:
print ("Oops! Was " + filename + " a directory? I won't download directories.")
print(str(i) + " files successfully downloaded.\n")
disconnect_ftp(ftp)
# Disconnects from ftp server
def disconnect_ftp(ftp):
print("Disconnecting from " + hostname + "...")
ftp.quit()
print("Disconnected from " + hostname + ".")
time.sleep(4)
hostname = "ftpsite" # TODO: Replace with the hostname of the server you want to connect to
username = "" # TODO: Replace the username
password = "" # TODO: Replace the password
source_directory = "/source/directory/" # TODO: Change location to wherever you want to start the download
destination_directory = "C:\example\FTP" # TODO: Change the location of where you want to download the files to
connect_ftp(hostname, username, password, source_directory, destination_directory)
答案 0 :(得分:-2)
为什么要使用Python?在* nix系统shell中,运行命令:
wget -r ftp://source/directory