使用os.listdir()创建文件列表

时间:2014-07-02 17:27:13

标签: python-2.7

我知道我可以使用os.listdir(path)创建一个给定目录内部的列表,但是如果我想知道在#34;中指定的目录中的目录内部是什么呢?路径"

我有一个包含25个目录的主目录;每个目录都包含30多个目录,并且在每个目录中有3-10个文件需要读入我的脚本。到目前为止,我已经尝试创建列表列表来记录所有目录,但它很快就会失控:

list = os.listdir(path)   # Path to the main directory
newList = []
for i in range(len(list)):
    newList.append(path + list[i])   # I modify the path so the next os.listdir command
                                     # finds the correct directory

list1 = []
for i in range(len(newList)):
    list1.append(os.listdir(newList[i]))    # Create a new list of lists for first set
                                            # of interior directories
list2 = []
for i in range(len(list1)):
    for j in range(len(list1[i])):
        list2.append(os.listdir(list1[i][j]))   # I'm encountering an error here

是否有我可以使用的命令将为我完成所有这些工作?我正在寻找一些东西,给定一个路径,它将返回其中所有目录的所有内容。任何建议都表示赞赏。

0 个答案:

没有答案
相关问题