3

コードの1つのステップで、テキストファイルのフォルダーを読み取り、それをdtmに変換するのに問題があります。問題は、何らかの理由で、私のコンピュータがディレクトリ内のテキストファイルとの接続を断続的にしか確立できないことです。返されるエラーは次のとおりです。

Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file '[file name]': No such file or directory

ただし、これらのファイルは、Pythonだけでなく、任意のテキストエディタで簡単に開くことができます。他の人ではなく時々接続を確立できるかもしれない理由はありますか?私のコードは以下の通りです:

files <- as.character(list.files(path="[file path]"))
readLines(files[1])  #here is where the error occurs, for example
n <- length(files) #this is my loop
subtitles <- character(n)
subtitle <- character(1)

for (i in 1:n){
   subtitle <- as.character(readLines(files[i]))
   subtitle <- iconv(subtitle, to="UTF8")
   subtitle <-  tolower(subtitle)
   subtitle <- as.character(paste(subtitle, collapse=" "))
   subtitles[i] <- subtitle[1]
}
4

2 に答える 2

9

List.filesフルパスのファイル名ではなく、ファイル名のみを提供します。試す

files <- as.character(list.files(path="[file path]"))
readLines(paste("[file path]",.Platform$file.sep,files[1],sep=""))
于 2012-04-12T04:35:34.810 に答える
1
directory <- "C://temp"  ## for example
filenames <- list.files(directory, pattern = "*.*", full.names = TRUE)
于 2014-08-18T04:24:43.550 に答える