使用R语言下载FTP链接时遇550错误的技术求助
FTP下载错误排查与解决请求
错误详情
尝试访问指定FTP链接时触发如下错误与警告:
Error in download.file(paste(url, filename, sep = ""), paste(getwd(), : cannot open URL 'ftp://gdo-dcp.ucllnl.org/pub/dcp/subset/202404050438gr5H_n_1msibTNA' 附加警告信息: In download.file(paste(url, filename, sep = ""), paste(getwd(), : cannot open URL 'ftp://gdo-dcp.ucllnl.org/pub/dcp/subset/202404050438gr5H_n_1msibTNA': FTP status was '550 Requested action not taken; file unavailable'
所用代码
library(RCurl) library(data.table) # Set the location where data should be saved setwd("D:/hydrology_climate/access0") # Change to the FTP url for the subset request. This is provided in the # DCP notification email (sent from no-reply@gdo4.llnl.gov). options(download.file.method="libcurl", url.method="libcurl") url = "ftp://gdo-dcp.ucllnl.org/pub/dcp/subset/202404050438gr5H_n_1msibT" filenames = getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE) filenames <- strsplit(filenames, "\r\n") filenames = unlist(filenames) tmp = data.table(filenames) zips = tmp[filenames %like% 'zip'] txts = tmp[filenames %like% 'txt'] nzips = as.numeric(nrow(zips)) for (i in 1:nzips) { filename = zips[i] download.file(paste(url, filename, sep = ""), paste(getwd(), "/", filename,sep = "")) } ntxts = as.numeric(nrow(txts)) for (i in 1:ntxts) { filename = txts[i] download.file(paste(url, filename, sep = ""), paste(getwd(), "/", filename,sep = "")) }
排查与解决步骤
- 修正FTP路径格式:当前
url变量末尾缺少斜杠,导致拼接文件名时生成错误路径。给url末尾添加/:url = "ftp://gdo-dcp.ucllnl.org/pub/dcp/subset/202404050438gr5H_n_1msibT/" - 优化路径拼接逻辑:用
file.path()替代手动拼接,避免分隔符错误:# 替换下载循环中的路径拼接代码 download.file(file.path(url, filename), file.path(getwd(), filename)) - 验证文件存在性:用FTP客户端(如FileZilla)或浏览器访问
url对应的目录,确认目标zip/txt文件确实存在,且文件名无拼写错误。 - 调整FTP连接参数:给
getURL添加被动模式参数,提升兼容性:filenames = getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE, ftp.passive = TRUE) - 替换下载函数:若
download.file仍报错,改用RCurl的curlDownload函数:curlDownload(file.path(url, filename), file.path(getwd(), filename))
内容的提问来源于stack exchange,提问作者Berama
相关产品推荐
相关产品推荐

