pythos 文件目录操作

来源:互联网 发布:网络号码发短信 编辑:程序博客网 时间:2024/05/21 05:42
import osimport shutilimport filecmpimport hashlibimport shutilfrom fnmatch import fnmatchdef create_checksum(path):    """    Reads in file. Creates checksum of file line by line.    Returns complete checksum total for file.    """    fp = open(path)    checksum = hashlib.md5()    while True:        buffer = fp.read(8192)        if not buffer:break        checksum.update(buffer)    fp.close()    checksum = checksum.digest()    return checksumclass 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    def CleanDir(self):        filelist=[]        filelist=os.listdir(self.path)        for f in filelist:            filepath = os.path.join(self.path, f)            if os.path.isfile(filepath):                os.remove(filepath)                print filepath+" removed!"            elif os.path.isdir(filepath):                shutil.rmtree(filepath,True)                print "dir "+filepath+" removed!"        os.removedirs(self.path)        return  True    def removeDir(self):        filelist=[]        filelist=os.listdir(self.path)        for f in filelist:            filepath = os.path.join( self.path, f )            if os.path.isfile(filepath):                os.remove(filepath)                print filepath+" removed!"            elif os.path.isdir(filepath):                shutil.rmtree(filepath,True)                print "dir "+filepath+" removed!"        os.removedirs(self.path)def removeDir(dir):    filelist=[]    filelist=os.listdir(dir)    for f in filelist:        filepath = os.path.join( dir, f )        if os.path.isfile(filepath):            os.remove(filepath)            print filepath+" removed!"        elif os.path.isdir(filepath):            shutil.rmtree(filepath,True)            print "dir "+filepath+" removed!"    os.removedirs(dir)def findfiles(filelist,filetype):    ff=[]    for f in filelist:        if fnmatch(f,filetype):            print f            ff.append(f)    return ffif __name__ == '__main__':    # if create_checksum('p1.py') == create_checksum('p2.py'):    #     print 'True'    # else:    #     print 'false'    d = DiskWalk("D:\\Java\\PyCharmWorkspace")    #print d.enumeratePaths()    filelist = d.enumeratePaths()    filetype="*.txt"    print findfiles(filelist,filetype)    #d.removeDir()    #removeDir("D:\\Java\\PyCharmWorkspace\\1")

0 0
原创粉丝点击