-2

我正在做一个需要返回树中所有叶子(文件)列表的项目。我不知道如何开始,我需要一些帮助:)

我需要创建一个程序来返回当前运行进程的文件夹中的所有文件和文件夹(my_program.py),结果应该包含根文件夹、文件、子文件夹和 subfolders.files 等。

4

2 回答 2

1
import os
##Provide value of a path in filepath variable
filepath="C:\Users\poonamr\Desktop"
for path, dirs, files in os.walk(os.path.abspath(filepath)):
    print path
    if len(dirs)==0:
        print('No directories available in "' + path + '"')
    else:
        print dirs
    if len(files)==0:
        print('No files available in "' + dirs + '"') 
    else:
        print files
    print "\n"
于 2016-11-17T09:10:49.230 回答
0
import os

def FileTree(Original_Path):
    dirlist=[]
    filelist=[]
    for dirnm in os.listdir(Original_Path):
        if os.path.isdir(Original_Path + "\\" + dirnm):
            dirlist.append(dirnm)
        else:
             filelist.append(dirnm)
    print "Folder    : " , Original_Path
    print "SubFolder : " , dirlist
    print "Files     : " , filelist
    print "\n\n"
    for dirSub in dirlist:
        FileTree(Original_Path+ "\\" + dirSub + "\\")


##Path specification    
Original_Path="C:\Users\poonamr\Desktop\Python Programs"
FileTree(Original_Path)
于 2016-11-18T06:07:09.297 回答