DiskWalk 遍历目录树工具类

import os

class DiskWalk(object):
    '''
    API for getting directory walking collections
    '''
    def __init__(self, path):
        if path is None or len(path) == 0:
            print 'Error: Please enter a valid path!'
            return False
        self.path = path
    def enumeratePaths(self):
        '''
        Returns the path to all the files in a directory as a list
        '''
        path_collection = []
        for dirpath, dirnames, filenames in os.walk(self.path):
            for file in filenames:
                fullpath = os.path.join(dirpath, file)
                path_collection.append(fullpath)
        return path_collection
    def enumerateFiles(self):
        '''
        Returns all the files in a directory as a list
        '''
        path_collection = []
        for dirpath, dirnames, filenames in os.walk(self.path):
            for file in filenames:
                path_collection.append(file)
        return path_collection
    def enumerateDir(self):
        '''
        Returns all the directories in a directory as a list
        '''
        path_collection = []
        for dirpath, dirnames, filenames in os.walk(self.path):
            for dir in dirnames:
                path_collection.append(dir)
        return path_collection
 

你可能感兴趣的:(python)