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
- pythos 文件目录操作
- perl 文件,目录操作
- 文件目录操作函数
- python文件目录操作
- 文件和目录操作
- 文件目录操作
- 文件和目录操作
- 文件目录操作集
- 文件目录操作编程
- 文件和目录操作
- python文件目录操作
- 文件/ 目录操作
- windows文件目录操作
- 文件目录操作命令
- 文件及目录操作
- Python 文件、目录操作
- C#文件目录操作
- python 目录,文件操作
- 硬盘组成介绍
- "_ABAddressBookCreate", referenced from: -[IFlyContact contact] in iflyMSC(IFlyContact.o)
- android自定义权限
- Android中的权限机制
- 用EXCEL打开CSV格式后,如何避免11位以上数字用科学计数法显示
- pythos 文件目录操作
- 2015-11-25 cocoapods 0.39.0 specs 下面的 13746 个项目的清单
- 整理 iOS 9 适配中出现的坑(图文)
- 构建轻量级webservice-Spring整合jaxws开发
- 误卸载linux自带的rpm包
- 虚拟机网络设置
- JAVA中的jna
- Mysql添加联合唯一键以及指定自增长的起始值
- Android超简易侧滑菜单的实现