__author__ = ' Baron ' import osimport codecsfrom os.path import join, Getsizedef getdirsize (Dirdict, rootpath): dirsize = 0l for root, dirs, files in os.walk (RootPath): if root == rootpath: for dir in dirs: dirdict, fsize = getdirsize (DirDict, join (RootPath, dir)) dirsize += fsize try: dirsize += sum ([GetSize (Join (ROOTPATh, file) for file in files]) except: pass if dirsize / 1024 / 1024 != 0 and rootpath not in dirdict: dirdict[ rootpath] = dirsize / 1024 / 1024 print len (dirdict) return dirdict, dirsizeif __name__ == ' __main__ ': write_path = ' c:\\users\\baron\\desktop\\log.txt ' write_file = codecs.open (write_path, ' W ', encoding= ' UTF-8 ') rootpath = ' c:\\ ' dirDict = {} For file in os. Listdir (RootPath): if file.startswith (' $ '): continue if os.path.isdir (Join (rootpath,file)): dirdict, fsize = getdirsize (Dirdict, join (rootpath, file)) seq_dict = sorted (DirDict.items (), Key=lambda t: t[1], reverse=true) for item in seq_dict: write_file.write (' \ t '. Join ([item[0], '%.1f ' % item[1]]). Decode (' GBK ') + ' \ n ')
Python analysis C-Disk large file [clip]