获取文件属性

文件操作 / 基本操作

file_path <- "file.txt"
moment <- as.POSIXct("2027-01-01 22:13:20",
                     tz = "UTC")
writeLines(c("Line 1", "Line 2"), file_path)
# Pin the modification time
Sys.setFileTime(file_path, moment)

# One data frame row: size, isdir, mtime, ...
info <- file.info(file_path)

size <- info$size
# size = 14
modified <- format(info$mtime)
# modified = 2027-01-01 22:13:20
is_dir <- info$isdir
# is_dir = FALSE

# Access modes: 4 is read, 2 is write
is_readable <- file.access(file_path, 4) == 0
# is_readable = TRUE
is_writable <- file.access(file_path, 2) == 0
# is_writable = TRUE

cat("size =", size, "\n")
cat("modified =", modified, "\n")
cat("is_dir =", is_dir, "\n")
cat("is_readable =", is_readable, "\n")
cat("is_writable =", is_writable, "\n")