获取 R 中的目录列表
R 编程语言中的目录或文件夹可能包含其他目录。可以使用 R 中的基本方法访问所有目录的列表,并将它们作为文件夹列表返回。
R 语言中的 list.dirs() 方法用于检索指定路径中存在的目录列表。返回的输出采用字符向量的形式,其中包含指定目录路径中包含的文件的名称,如果没有返回目录,则返回 null。如果指定的路径不存在或不是目录或不可读,则从评估中跳过。
Syntax:
list.dirs(path = “.”, full.names = TRUE, recursive = TRUE)
Parameter:
path – The path of the main directory
full.names (Default : TRUE) – If set to FALSE, only the names of the subdirectories are displayed. Else, the entire paths are displayed.
Returns: List of files in lexicographic order
使用目录:
获取R中目录的完整路径
当属性 full.names = TRUE 时返回的完整路径。如果我们设置,recursive = TRUE,则目录按层次顺序返回,从主目录路径开始,然后是子目录。但是,如果我们指定 recursive = FALSE,则只返回子目录。
例子:
R
# specifying the path
main_dir <- "/Users/mallikagupta/Desktop/gfg"
print ("list of directories including the main directory")
dir_list <- list.dirs(main_dir,recursive = TRUE)
print (dir_list)
print ("list of directories excluding the main directory")
dir_list <- list.dirs(main_dir,recursive = FALSE)
print (dir_list)
R
main_dir <- "/Users/mallikagupta/Desktop/gfg"
print ("list of directories")
dir_list <- list.dirs(main_dir)[- 1]
print (dir_list)
R
main_dir <- "/Users/mallikagupta/Desktop/gfg"
print ("list of directory names")
dir_list <- list.dirs(main_dir,full.names = FALSE,
recursive = FALSE)
print (dir_list)
R
main_dir <- "/Users/mallikagupta/Desktop/gfg"
# set working director
setwd(main_dir)
print ("list of directories")
dir_list <- list.dirs()[- 1]
# getting folder names
folder_list <- gsub("\\./", "", dir_list)
print (folder_list)
输出
“list of directories including the main directory”
[1] “/Users/mallikagupta/Desktop/gfg” “/Users/mallikagupta/Desktop/gfg/Interviews”
[3] “/Users/mallikagupta/Desktop/gfg/Placements” “/Users/mallikagupta/Desktop/gfg/WFH”
“list of directories excluding the main directory”
[1] “/Users/mallikagupta/Desktop/gfg/Interviews” “/Users/mallikagupta/Desktop/gfg/Placements”
[3] “/Users/mallikagupta/Desktop/gfg/WFH”
另一种排除主目录的方法是指定附加到 list.dirs() 方法的 [-1] 索引,该方法仅返回子目录。
例子:
电阻
main_dir <- "/Users/mallikagupta/Desktop/gfg"
print ("list of directories")
dir_list <- list.dirs(main_dir)[- 1]
print (dir_list)
输出
“list of directories”
[1] “/Users/mallikagupta/Desktop/gfg/Interviews” “/Users/mallikagupta/Desktop/gfg/Placements”
[3] “/Users/mallikagupta/Desktop/gfg/WFH”
在 R 中获取文件夹名称
可以仅检索主目录中的文件夹或目录名称。在这种情况下,我们在方法调用中设置参数 full.names = FALSE。名称按字母顺序排列。
例子:
电阻
main_dir <- "/Users/mallikagupta/Desktop/gfg"
print ("list of directory names")
dir_list <- list.dirs(main_dir,full.names = FALSE,
recursive = FALSE)
print (dir_list)
输出
“list of directory names”
[1] “Interviews” “Placements” “WFH”
gsub()函数用于替换字符串模式的所有匹配项。我们指定的模式相当于一个正则表达式(“\\./”),用于从完整路径名中删除所有先前的段并仅返回路径的最后一部分,即文件夹名称。
Syntax:
gsub(pattern, replacement, string)
Parameter:
pattern – The pattern to locate in the string
replacement – The string to replace the matches with.
例子:
电阻
main_dir <- "/Users/mallikagupta/Desktop/gfg"
# set working director
setwd(main_dir)
print ("list of directories")
dir_list <- list.dirs()[- 1]
# getting folder names
folder_list <- gsub("\\./", "", dir_list)
print (folder_list)
输出
“list of directories”
[1] “Interviews” “Placements” “WFH”