如何检查文件是否已存在于 R 中?
在本文中,我们将讨论如何使用 R 编程语言检查文件是否已存在。
使用目录:
方法一:使用 File.exists()
函数file.exists() 返回一个逻辑向量,指示函数提到的文件是否存在。
注意:确保为那些提供文件路径,而不是在当前工作目录中。
Syntax: File.exists(“file_path”)
Return value: The function file.exists() returns a logical vector indicating whether the files named by its argument exist.
- TRUE- file exists
- FALSE- file does not exists
例子:
R
file.exists("Akshit.R")
R
file.exists("GFG.R")
R
#"-f" (existence and not
# being a directory)
file_test("-f","Akshit.R")
R
file_test("-f","GFG.R")
输出:
[1] TRUE
示例 2:
电阻
file.exists("GFG.R")
输出:
[1] FALSE
方法 2:使用 file_test()
此函数还可用于检查文件是否存在。
Syntax: file_test(op, x, y)
Parameter:
- op: a character string specifying the test to be performed. Unary tests (only x is used) are “-f” (existence and not being a directory), “-d” (existence and directory) and “-x” (executable as a file or searchable as a directory). Binary tests are “-nt” (strictly newer than, using the modification dates) and “-ot” (strictly older than): in both cases the test is false unless both files exist.
- x, y: character vectors giving file paths.
Returns: The function file_test() returns a logical vector indicating whether the files named by its argument exist.
- TRUE- file exists
- FALSE- file does not exists
例子
电阻
#"-f" (existence and not
# being a directory)
file_test("-f","Akshit.R")
输出
[1] TRUE
示例 2:
电阻
file_test("-f","GFG.R")
输出
[1] TRUE