我有一个包含许多其他文件夹的文件夹。我希望能够提取这些目录名称并将它们分配给变量。我不知道那些文件夹的名称,也不知道数量。我只需要这些文件夹的名称,而不是整个路径。
例子:
在 \users\temp\ 里面我有 2 个目录。 test1, test2
folder[0] = test1
folder[1] = test2
等等....取决于我有多少个目录。
谢谢
请您参考如下方法:
您可以使用 os.listdir 和列表理解:
import os
path = r"\users\temp" #use raw string as otherwise \t will be converted to tab space
folder = [x for x in os.listdir(path) if os.path.isdir(os.path.join(path,x))]
