简体   繁体   English

File.exists()不起作用

[英]File.exists() doesn't work

I have to be either blind or stupid, but I cannot move forward with this: 我必须是盲人或愚蠢的,但是我不能继续前进:

I have a variable sourceName defined as: 我有一个变量sourceName定义为:

sourceName = file.path(dataDatabase, dates[1], paste0(exchange, "_", ticker, "_trade.csv"))

which is this string: 这是这个字符串:

 "/home/malejg/goxtrader/data/2014-12-12/Bitfinex_BTC_trade.csv"

When I check the existenece of the file, I got the following result: 当我检查文件的存在时,得到以下结果:

> file.exists(sourceName)
> [1] TRUE

so the file definitely exists. 因此该文件肯定存在。 But when I use the above code in the if statement going like: 但是当我在if语句中使用上面的代码时:

 if(file.exists(sourceName)){
    some sample code here}

it throws an error: 它抛出一个错误:

 Error in if (file.exists(sourceName)) { : argument is of length zero

How is it possible? 这怎么可能? The code works fine on Windows, so is there some Linux related problem? 该代码在Windows上可以正常运行,因此是否存在一些与Linux相关的问题?

SESSION INFO: 会议信息:

sessionInfo() R version 3.1.2 (2014-10-31) Platform: x86_64-pc-linux-gnu (64-bit) sessionInfo()R版本3.1.2(2014-10-31)平台:x86_64-pc-linux-gnu(64位)

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C 区域设置:[1] LC_CTYPE = en_US.UTF-8 LC_NUMERIC = C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C [3] LC_TIME = en_US.UTF-8 LC_COLLATE = en_US.UTF-8 [5] LC_MONETARY = en_US.UTF-8 LC_MESSAGES = en_US.UTF-8 [7] LC_PAPER = en_US.UTF-8 LC_NAME = C [9] LC_ADDRESS = C LC_TELEPHONE = C [11] LC_MEASUREMENT = zh_CN.UTF-8 LC_IDENTIFICATION = C

attached base packages: [1] stats graphics grDevices utils 附加的基本软件包:[1]统计图形grDevices utils
datasets methods base 数据集方法库

other attached packages: [1] data.table_1.9.4 xts_0.9-7 其他随附的软件包:[1] data.table_1.9.4 xts_0.9-7
zoo_1.7-11 zoo_1.7-11

loaded via a namespace (and not attached): [1] chron_2.3-45 通过名称空间(未附加)加载:[1] chron_2.3-45
grid_3.1.2 lattice_0.20-30 plyr_1.8.1 [5] Rcpp_0.11.5 grid_3.1.2点阵_0.20-30 plyr_1.8.1 [5] Rcpp_0.11.5
reshape2_1.4.1 stringr_0.6.2 tools_3.1.2 reshape2_1.4.1 stringr_0.6.2 tools_3.1.2

This is an extremely speculative answer... 这是一个非常投机的答案。

I can get file.exists to return a logical vector of zero length only as follows: 我可以让file.exists返回零长度的逻辑向量,如下所示:

> file.exists(character(0))
logical(0)

So my tentative hypothesis is that you mistakenly wiped that variable that should have been a file path and passed a zero length character vector to file.exists by mistake. 因此,我的临时假设是您错误地擦除了本应为文件路径的变量,并将零长度字符向量传递给file.exists

This would imply that isTRUE is the safest option in one sense, but personally I can hardly think of a case where passing a zero length vector to file.exists would be intentional on my part, and I'd probably want to see that error and fix it. 从某种意义上讲,这意味着isTRUE 最安全的选项,但就我个人而言,我很难想到将零长度向量传递给file.exists情况对我而言是故意的,我可能想看看该错误并修理它。

You should use isTRUE() 您应该使用isTRUE()

if (isTRUE(file.exists(sourceName))) {
    cat('Found')
}

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

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