kek*_*es4 1 python directory recursion
我的程序不相信文件夹是目录,假设它们是文件,并且因此,递归将文件夹打印为文件,然后由于没有等待遍历的文件夹,程序结束.
import os
import sys
class DRT:
def dirTrav(self, dir, buff):
newdir = []
for file in os.listdir(dir):
print(file)
if(os.path.isdir(file)):
newdir.append(os.path.join(dir, file))
for f in newdir:
print("dir: " + f)
self.dirTrav(f, "")
dr = DRT()
dr.dirTrav(".", "")
Run Code Online (Sandbox Code Playgroud)
从那里看os.walk:
此示例显示起始目录下每个目录中的非目录文件占用的字节数,但它不在任何CVS子目录下查看:
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print root, "consumes",
print sum(getsize(join(root, name)) for name in files),
print "bytes in", len(files), "non-directory files"
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2182 次 |
| 最近记录: |