What is the Python way to walk a directory tree?

后端 未结 11 1599
走了就别回头了
走了就别回头了 2020-12-06 09:52

I feel that assigning files, and folders and doing the += [item] part is a bit hackish. Any suggestions? I\'m using Python 3.2

from os import *
from os.pat         


        
11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 10:11

    def dir_contents(path):
        files,folders = [],[]
        for p in listdir(path):
            if isfile(p): files.append(p)
            else: folders.append(p)
        return files, folders
    

提交回复
热议问题