pythos 文件目录操作

import os
import shutil
import filecmp
import hashlib
import shutil
from fnmatch import fnmatch
def 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 checksum
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
    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 ff

if __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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值