且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R测试文件是否存在,是否不是目录

更新时间:2023-11-29 15:34:58

使用 file_test()

这提供了外壳样式的文件测试,并且可以区分文件夹中的文件。

This gives shell-style file tests, and can distinguish files from folders.

例如

input.good = "~/directory/file.txt"
input.bad = "~/directory/"

file_test("-f", input.good) # returns TRUE
file_test("-f", input.bad) #returns FALSE

从手册中:


用法

file_test(op,x,y)参数

file_test(op, x, y) Arguments

op >指定要执行的测试的字符串。一元
测试(仅使用x)是 -f(存在而不是目录),
-d(存在和目录)和 -x(可作为文件执行或
可作为目录搜索)。二进制测试分别是 -nt(使用修改日期比
严格更新)和 -ot(必须比使用日期更早):
在两种情况下,除非两个文件都存在,否则测试均为假。

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 字符向量提供文件路径。

x, y character vectors giving file paths.