R语言中的path_ext_set
函数用于修改文件的后缀名。下面是一个简单的示例,展示如何使用该函数以及相关的源代码。
# 设置文件后缀名
path_ext_set <- function(file_path, new_ext) {
# 检查文件路径是否存在
if (!file.exists(file_path)) {
stop("File path does not exist.")
}
# 获取文件名和旧的后缀名
file_name <- basename(file_path)
old_ext <- tools::file_ext(file_name)
# 根据新的后缀名生成新的文件名
new_name <- paste0(file_path, ".", new_ext)
# 重命名文件
file.rename(file_path, new_name)
# 返回新的文件路径
return(new_name)
}
# 示例用法
file_path <- "path/to/file.txt" # 文件路径
new_ext <- "csv" # 新的后缀名
# 修改文件后缀名
new_file_path <- path_ext_set(file_path, new_ext)
print(paste("文件路径修改前:", file_path))
print(paste("文件路径修改后:", new_file_path))
在上述代码中,我们定义了一个名为path_ext_set