0
下面的脚本会从ftp站点不是我想要下载的实际文件只下载的文件名下载的所有文件,为什么文件大小保持在下载后为0。你有什么想法是什么导致问题?任何帮助将不胜感激。Python的 - 从ftp目录
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)
下载目录中的所有文件在本质上是一样的下载目录,所有你需要做的就是先创建一个目标目录具有相同的名称作为一个上源文件的来源。 – martineau
感谢您的建议@马蒂诺。 – user
它下载文件,但文件大小为0 kb。 “0个文件成功下载”。 – user