从文件夹中读取文件,尽管索引正确,但我收到“文件错误(文件,“rt"):无法打开连接"错误 [英] Reading files from a folder and despite indexing correctly, I receive a "Error in file(file, "rt") : cannot open the connection" error
问题描述
我正在尝试从文件夹中读取文件.我设置了工作目录和目标文件夹.
I am trying to read files from a folder. I set the working directory and the target folder.
setwd("F://directory/SimulationOutputs/folder - Copy/")
spwfolder <- ("F:/directory/SimulationOutputs/folder - Copy/spws/")
spws <- list.files(spwfolder)
在这一步之后,我检查它是否正确:
After this step, I check to see if it is getting it right:
spws[1]
[1] "Cambridge2018_136.txt"
接下来,我要读取第一个文件:
Next, I want to read the first file:
spw<-read.table(spws[1], sep=";", row.names=NULL, skip = 8, fill = TRUE,
header=TRUE, stringsAsFactors = FALSE)
我收到这条消息:
文件错误(文件,rt"):无法打开连接另外:警告消息:在文件(文件,rt")中:无法打开文件'Cambridge2018_136.txt':没有那个文件或目录
Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'Cambridge2018_136.txt': No such file or directory
我做错了什么?提前致谢!
What am I doing wrong? Thanks in advance!
推荐答案
您将工作目录设置为 /folder - Copy/
但您的文件在 /folder - Copy/spws/
.
You set the working directory to /folder - Copy/
but your files are in /folder - Copy/spws/
.
或者,您可以将 list.files
与 full.names=TRUE
一起使用,这样 spws
将包含文件的完整路径和那么你的工作目录在哪里设置就无关紧要了.
Alternately, you could use list.files
with full.names=TRUE
so that spws
will contain the full path to the file and then it wouldn't matter where your working directory is set.
这篇关于从文件夹中读取文件,尽管索引正确,但我收到“文件错误(文件,“rt"):无法打开连接"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!