我从重命名文件时遇到问题R
。
在我的桌面文件夹中,有10个文件:
račun 1.xlsx
račun 2.xlsx
...
račun 10.xlsx
我尝试了以下方法:
files <- list.files(path = "myfolder")
file.rename(files,
paste0("novi_", 1:10, ".xlsx"))
这就是我得到的结果:
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
我想这是因为Unicode字符č,但是我不知道如何找到解决方案。
编辑2:此处的解决方案是OP更改“控制面板”中的“区域”设置,将格式设置为塞尔维亚语(拉丁语,塞尔维亚)。
编辑1:请参阅注释:OP在Windows计算机上。这里的问题是list.files()
(大概是dir()
因为它们调用相同的.Internal)正在将非ASCII文件名转换为ASCII,但是Windows希望file.exists()
向它发送unicode文件名,(大概也是file.rename()
)
尝试:
file.rename(gsub("c", "č", files), paste0("novi_", seq_along(files, ".xlsx"))
# could work, but it didn't for `file.exists()`
原始答案:
setwd(<your path>)
(files <- list.files())
# [1] "račun 1.xlsx" "račun 2.xlsx" "račun 3.xlsx" "račun 4.xlsx" "račun 5.xlsx [6] "račun 6.xlsx"
file.rename(files, paste0("novi_", seq_along(files, ".xlsx"))
# [1] TRUE TRUE TRUE TRUE TRUE TRUE
您在中指定了路径的事实list.files()
表明您不在正确的目录中
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句