确定字符是否为有效文件路径的最佳方法是什么?因此,CheckFilePath( "my*file.csv")将返回FALSE (在windows上*是无效字符),而CheckFilePath( "c:\\users\\blabla\\desktop\\myfile.csv" )将返回TRUE。
请注意,文件路径可以是有效的,但不存在于磁盘上。
发布于 2011-10-16 01:13:39
这是save用来执行该功能的代码:
....
else file(file, "wb")
on.exit(close(con))
}
else if (inherits(file, "connection"))
con <- file
else stop("bad file argument")
......发布于 2011-10-16 00:33:15
也许file.exists()就是你要找的?在帮助页面中:
file.exists returns a logical vector indicating whether the files named by its argument exist.
(Here ‘exists’ is in the sense of the system's stat call: a file will be reported as existing only
if you have the permissions needed by stat. Existence can also be checked by file.access, which
might use different permissions and so obtain a different result. 还可以使用其他几个函数来访问计算机的文件系统,也可以在帮助页面中引用这些函数。
发布于 2011-10-16 00:44:02
不,没有办法(可靠地)做到这一点。我在Windows和Linux中都看不到可以测试这一点的操作系统接口。您通常会尝试创建文件并得到失败消息,或者尝试读取文件并得到一种“不存在”的消息。
因此,您应该依靠操作系统来告诉您是否可以对文件执行您想要执行的操作(通常是读和/或写)。
我想不出比测验(“输入有效的完全限定的Windows文件路径:”)更想知道这一点的原因了。
https://stackoverflow.com/questions/7779032
复制相似问题