使用Loop

时间:2018-09-03 18:36:52

标签: r loops download ftp

我正在尝试下载FTP文件夹中的所有文件

temp <- tempfile()
destination <- "D:/test"

url <- "ftp://XX.XX.net/"
userpwd <- "USER:Password"
filenames <- getURL(url, userpwd = userpwd,ftp.use.epsv = FALSE,dirlistonly = TRUE)     
filenames <- strsplit(filenames, "\r*\n")[[1]]

当我打印“文件名”时,我得到了FTP文件夹中的所有文件名-直到此处为止都是正确的输出

[1] "2018-08-28-00.gz" "2018-08-28-01.gz"
[3] "2018-08-28-02.gz" "2018-08-28-03.gz"
[5] "2018-08-28-04.gz" "2018-08-28-05.gz"
[7] "2018-08-28-08.gz" "2018-08-28-09.gz"
[9] "2018-08-28-10.gz" "2018-08-28-11.gz"
[11] "2018-08-28-12.gz" "2018-08-28-13.gz"
[13] "2018-08-28-14.gz" "2018-08-28-15.gz"
[15] "2018-08-28-16.gz" "2018-08-28-17.gz"
[17] "2018-08-28-18.gz" "2018-08-28-23.gz"

for ( i in filenames ) {
    download.file(paste0(url,i), paste0(destination,i), mode="w")    
}

我收到此错误

  trying URL 'ftp://XXX.net/2018-08-28-00.gz'
  Error in download.file(paste0(url, i), paste0(destination, i), mode = "w") : 
    cannot open URL 'ftp://XXX.net/2018-08-28-00.gz'
  In addition: Warning message:
  In download.file(paste0(url, i), paste0(destination, i), mode = "w") :
    InternetOpenUrl failed: 'The login request was denied'

我将代码修改为

 for ( i in filenames )
 {
   #download.file(paste0(url,i), paste0(destination,i), mode="w")

 download.file(getURL(paste(url,filenames[i],sep=""), userpwd = 
 "USER:PASSWORD"), paste0(destination,i), mode="w")

}

之后,我得到了这个错误

Error in function (type, msg, asError = TRUE)  : RETR response: 550

1 个答案:

答案 0 :(得分:0)

没有minimal, complete, and verifiable example,直接复制您的问题是一个挑战。假设文件名不包含URL,则需要将它们组合以访问文件。

download.file()要求读取文件,输出文件以及有关是否要下载二进制文件的其他标志。

例如,我在我的Github网站上存储了来自Alberto Barradas的Pokémonstats kaggle.com数据集的数据。要将某些文件下载到R工作目录的test子目录中,可以使用以下代码:

filenames <- c("gen01.csv","gen02.csv","gen03.csv")
fileLocation <- "https://raw.githubusercontent.com/lgreski/pokemonData/master/"
# use ./ for subdirectory of current directory, end with / to work with paste0()
destination <- "./test/"
# note that these are character files, so use mode="w" 
for (i in filenames){
     download.file(paste0(fileLocation,i),
                          paste0(destination,i),
                          mode="w")

}

...以及输出: enter image description here

paste0()函数将没有空格的文本连接起来,这允许代码为每个源文件的URL以及将存储目标文件的子目录生成完全限定的路径名​​。

为了说明paste0()循环中for()的情况,我们可以使用message()打印到R控制台。

> # illustrate what paste0() does
> for (i in filenames){
+      message(paste("Source is: ",paste0(fileLocation,i)))
+      message(paste("Destination is:",paste0(destination,i)))
+ }
Source is:  https://raw.githubusercontent.com/lgreski/pokemonData/master/gen01.csv
Destination is: ./test/gen01.csv
Source is:  https://raw.githubusercontent.com/lgreski/pokemonData/master/gen02.csv
Destination is: ./test/gen02.csv
Source is:  https://raw.githubusercontent.com/lgreski/pokemonData/master/gen03.csv
Destination is: ./test/gen03.csv
>