我想在输出中递归显示文件和文件夹结构。
实际结构:
Root--|
|
DIRA--|
|
DIRC--File5
File3
File4
File1
File2
DIRB--|
|
No File
预期输出:
Root:
File1
File2
Root/DIRA
File3
File4
Root/DIRA/DIRC
File5
Root/DIRB
No File Found
我已经为下面编写了以下代码。但需要输入以及如何修改它以获得所需的输出。
代码
import os.path
path = 'C:\\My\\path\\here'
for root, dirnames, filenames in os.walk(path):
for subdirname in dirnames:
print subdirname
for filename in filenames:
print os.path.join(root, filename)
实际输出
DIRA
DIRB
C:\My\path\here\File1
C:\My\path\here\File2
DIRC
C:\My\path\here\DIRA\File3
C:\My\path\here\DIRA\File4
C:\My\path\here\DIRA\DIRC\File5
答案 0 :(得分:0)
import os
path = 'Root'
for root, dirnames, filenames in os.walk(path):
print root
for filename in filenames:
print filename
if not filenames:
print 'No File Found'
print