要从路径中提取每个文件夹名称,可以使用编程语言中的字符串处理函数。以下是几种常见编程语言的示例代码:
import os
path = "/home/user/documents/images"
folders = os.path.normpath(path).split(os.path.sep)
print(folders)
const path = "/home/user/documents/images";
const folders = path.split("/").filter(folder => folder !== "");
console.log(folders);
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String path = "/home/user/documents/images";
String[] folders = path.split("/");
System.out.println(Arrays.toString(folders));
}
}
using System;
class Program {
static void Main() {
string path = "/home/user/documents/images";
string[] folders = path.Split('/');
Console.WriteLine(string.Join(", ", folders));
}
}
这些示例代码将路径字符串按照路径分隔符(如 /
或 \
)分割,并将结果存储在一个数组或列表中。然后,可以遍历这个数组或列表来访问每个文件夹名称。
领取专属 10元无门槛券
手把手带您无忧上云