简体   繁体   English

如何在R中的循环内重命名多个文件

[英]How to rename multiple files inside a loop in R

I have downloaded one photo of each deputy. 我为每位代表下载了一张照片。 In total, I have 513 photos (but I hosted a file with 271 photos). 总共有513张照片(但我托管了271张照片)。 Each photo was named with the ID of the deputy. 每张照片都用副ID命名。 I want to change the name of photo to the deputy's name. 我想将照片名称更改为代理人的名字。 This means that "66179.jpg" file would be named "norma-ayub.jpg". 这意味着“ 66179.jpg”文件将被命名为“ norma-ayub.jpg”。

I have a column with the IDs ("uri") and their names ("name_lower"). 我有一列包含ID(“ uri”)及其名称(“ name_lower”)的列。 I tried to run the code with "destfile" of download.file(), but it receives only a string. 我试图用download.file()的“ destfile”运行代码,但它仅接收一个字符串。 I couldn't find out how to work with file.rename(). 我找不到如何使用file.rename()的方法。

And rename_r_to_R changes only the file extension. 重命名_r_to_R仅更改文件扩展名。

I am a beginner in working with R. 我是与R合作的初学者。

CSV file: https://gist.github.com/gabrielacaesar/3648cd61a02a3e407bf29b7410b92cec CSV文件: https//gist.github.com/gabrielacaesar/3648cd61a02a3e407bf29b7410b92cec

Photos: https://github.com/gabrielacaesar/studyingR/blob/master/chamber-of-deputies-17jan2019-files.zip (It's not necessary to download the ZIP file; When running the code below, you also get the photos, but it takes some time to download them) 图片: https : //github.com/gabrielacaesar/studyingR/blob/master/chamber-of-deputies-17jan2019-files.zip (无需下载ZIP文件;运行下面的代码时,您也会得到照片,但是下载它们需要一些时间)

deputados <- fread("dep-legislatura56-14jan2019.csv")

i <- 1

while(i <= 514) {
  tryCatch({
    url <- deputados$uri[i]
    api_content <- rawToChar(GET(url)$content)
    pessoa_info <- jsonlite::fromJSON(api_content)
    pessoa_foto <- pessoa_info$dados$ultimoStatus$urlFoto
    download.file(pessoa_foto, basename(pessoa_foto), mode = "wb")
    Sys.sleep(0.5)
  }, error = function(e) return(NULL)
  )
  i <- i + 1
}

I downloaded the files you provided and directly read them into R or unzipped them into a new folder respectivly: 我下载了您提供的文件,并将它们直接读入R或分别将其解压缩到新文件夹中:

df <- data.table::fread(
  "https://gist.githubusercontent.com/gabrielacaesar/3648cd61a02a3e407bf29b7410b92cec/raw/1d682d8fcdefce40ff95dbe57b05fa83a9c5e723/chamber-of-deputies-17jan2019", 
  sep = ",",
  header = TRUE)
download.file("https://github.com/gabrielacaesar/studyingR/raw/master/chamber-of-deputies-17jan2019-files.zip",
              destfile = "temp.zip")
dir.create("photos")
unzip("temp.zip", exdir = "photos")

Then I use list.files to get the file names of all photos, match them with the dataset and rename the photos. 然后,我使用list.files来获取所有照片的文件名,将它们与数据集匹配并重命名照片。 This runs very fast and the last bit will report if renaming the file was succesful. 这运行非常快,最后一位将报告文件重命名是否成功。

photos <- list.files(
  path = "photos", 
  recursive = TRUE,
  full.names = TRUE
)

for (p in photos) {
  id <- basename(p)
  id <- gsub(".jpg$", "", id)
  name <- df$name_lower[match(id, basename(df$uri))]
  fname <- paste0(dirname(p), "/", name, ".jpg")
  file.rename(p, fname)

# optional
  cat(
    "renaming", 
    basename(p), 
    "to", 
    name, 
    "succesful:", 
    ifelse(success, "Yes", "No"),
    "\n"
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM