我们可以使用ftplib在Python中提供完整的FTP支持。我试图通过递归传递根目录名来从几个根目录下载所有文件
import sys
import ftplib
import os
from ftplib import FTP
try:
ftp=FTP("ftpservice.example.com","username","password")
except EOFError as e:
raise Exception('Disconnection event...!!!')
def downloadFiles(path,destination):
dest_path = ""
try:
ftp.cwd(path)
os.chdir(destination)
dest_path = destination[0:len(destination)-1]+path
os.mkdir(dest_path)
except OSError:
print "%s alredy exist"%path
except ftplib.error_perm:
raise Exception("error: Invalid entry- could not change to ",path)
filelist=ftp.nlst()
for file in filelist:
try:
ftp.cwd(path+file+"/")
downloadFiles(path+file+"/",destination)
except ftplib.error_perm:
os.chdir(destination[0:len(destination)-1]+path)
ftp.retrbinary("RETR "+file, open(os.path.join(dest_path,file),"wb").write)
print file + " downloaded"
return
if __name__ == '__main__':
sources = ["ab","aldf","amrdf","lient","BF"] #root folder of the file tree
dest="/home/name/ftp_data/" # place to which the files are download
for source in sources:
source = "/%s/"%source
downloadFiles(source,dest)
print "\nDownload Completed... !!!"
程序执行并下载了很多文件,但在下载之间显示
ftplib.error_perm: 550 companymaster.ace: The system cannot find the file specified.
为什么会这样呢?
实际上我的程序列出了目录下的所有项目,如果该项目不是目录,则下载每个项目。
但我得到“系统找不到指定的文件”。 我真的很困惑。
任何帮助?