打印整个目录树 [英] Print out the whole directory tree
本文介绍了打印整个目录树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在的代码是:
import os
Tree = {}
Tree = os.listdir('Dir')
>>>打印(树)
['新建文件夹' Textfile1.txt','Textfile2.txt']
没有打印出子目录。 (新文件夹是一个子目录)
我的问题是,我如何输出目录中的所有文件和子目录中的文件?
解决方案
import os
def Test1(rootDir):
list_dirs = os.walk(rootDir )
for root,dirs,list_dirs中的文件:d b中的d b
print os.path.join(root,d)
文件中的f:
print os.path.join(root,f)
OR:
import os
pre>
def Test2(rootDir):
在os.listdir(rootDir)中的列表:
path = os .path.join(rootDir,lists)
打印路径
如果os.path.isdir(路径):
Test2(路径)
对于测试文件树:
E:\ TEST
│ - A
││ - AA
│││ - AAA.txt
││ - AB.txt
││ - AC
│││- -ABA.txt
││ - AD.txt
│ - B.txt
│ - C
││ - CA.txt
││ --CB.txt
│ - D.txt
│ - E
运行以下代码:
Test1('E:\TEST')
print'===
Test2('E:\TEST' )
您可以看到结果有区别:
>>>
E:\TEST\A
E:\TEST\C
E:\TEST\E
E:\TEST\B.txt
E:\TEST\D.txt
E:\TEST\A\AA
E:\TEST\A\AC
E: \TEST\A\AB.txt
E:\TEST\A\AD.txt
E:\TEST\A\AA\AAA.txt
E:\TEST\A\AC\ABA.txt
E:\TEST\C\CA.txt
E:\TEST\C\CB .txt
=====================================
E :\TEST\A
E:\TEST\A\AA
E:\TEST\A\AA\AAA.txt
E:\ TEST\A \AB.txt
E:\TEST\A\AC
E:\TEST\A\AC\ABA.txt
E: \TEST\A\AD.txt
E:\TEST\B.txt
E:\TEST\C
E:\TEST\C\\ \\ CA.txt
E:\TEST\C\CB.txt
E:\TEST\D.txt
E:\TEST\E
>>>
将其保存在列表中:
import os
files = []
def Test1(rootDir):
files.append(rootDir)
list_dirs = os.walk rootDir)
用于root,dirs,list_dirs中的文件:d b中的d b
$ b file.append(os.path.join(root,d))
for f in文件:
files.append(os.path.join(root,f))
import os
files = [rootDir]
def Test2(rootDir):
os.listdir(rootDir)中的列表:
path = os.path.join(rootDir,lists)
files.append(path)
如果os.path.isdir (路径):
Test2(路径)
The code i have now:
import os Tree = {} Tree = os.listdir('Dir')
>>>print(Tree) ['New Folder', 'Textfile1.txt', 'Textfile2.txt']
That doesn't print out the files in the subdirectories. (New Folder is a subdirectory).
My question is, how can i output all the files in the directory and the files in subdirectories?
解决方案import os def Test1(rootDir): list_dirs = os.walk(rootDir) for root, dirs, files in list_dirs: for d in dirs: print os.path.join(root, d) for f in files: print os.path.join(root, f)
OR:
import os def Test2(rootDir): for lists in os.listdir(rootDir): path = os.path.join(rootDir, lists) print path if os.path.isdir(path): Test2(path)
For the test file tree:
E:\TEST │--A │ │--A-A │ │ │--A-A-A.txt │ │--A-B.txt │ │--A-C │ │ │--A-B-A.txt │ │--A-D.txt │--B.txt │--C │ │--C-A.txt │ │--C-B.txt │--D.txt │--E
Running the following code:
Test1('E:\TEST') print '=======================================' Test2('E:\TEST')
You can see there are difference between the results:
>>> E:\TEST\A E:\TEST\C E:\TEST\E E:\TEST\B.txt E:\TEST\D.txt E:\TEST\A\A-A E:\TEST\A\A-C E:\TEST\A\A-B.txt E:\TEST\A\A-D.txt E:\TEST\A\A-A\A-A-A.txt E:\TEST\A\A-C\A-B-A.txt E:\TEST\C\C-A.txt E:\TEST\C\C-B.txt ======================================= E:\TEST\A E:\TEST\A\A-A E:\TEST\A\A-A\A-A-A.txt E:\TEST\A\A-B.txt E:\TEST\A\A-C E:\TEST\A\A-C\A-B-A.txt E:\TEST\A\A-D.txt E:\TEST\B.txt E:\TEST\C E:\TEST\C\C-A.txt E:\TEST\C\C-B.txt E:\TEST\D.txt E:\TEST\E >>>
To save them in a list:
import os files = [] def Test1(rootDir): files.append(rootDir) list_dirs = os.walk(rootDir) for root, dirs, files in list_dirs: for d in dirs: files.append(os.path.join(root, d)) for f in files: files.append(os.path.join(root, f)) import os files = [rootDir] def Test2(rootDir): for lists in os.listdir(rootDir): path = os.path.join(rootDir, lists) files.append(path) if os.path.isdir(path): Test2(path)
这篇关于打印整个目录树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文