我正在开发一个需要返回树中所有叶子(文件)列表的项目。我不知道如何开始,我需要一些帮助:)
我需要创建一个程序来返回运行进程(my_program.py)的当前文件夹中的所有文件和文件夹,结果应包含根文件夹,文件,子文件夹和子文件夹。文件等....
答案 0 :(得分: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"
答案 1 :(得分: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)