以字节为单位获取文件mod time和size(python)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以字节为单位获取文件mod time和size(python)相关的知识,希望对你有一定的参考价值。
#!/bin/env python import os, time, re, optparse usage = ''' ''' parser = optparse.OptionParser(usage=usage, version='%prog v0.2') parser.add_option('--path', dest='path', help='provided path to log dir.') #parser.add_option('--', dest='', help='') (options, args) = parser.parse_args() # # This function takes a top level path as argument then generates a file list with up to 1 deep nested directories. # Filenames containing only numbers are not added to this list # questions: # Will sorting lists prior to for loop make it run faster? initial time to scan buildbot logdirs: 23 minutes # def genFileList(path): startTime = time.time() filelistA = os.listdir(path) filelistB = list() filelistC = list() for file in filelistA: if not file.isdigit() and not os.path.isdir(os.path.join(path, file)): filelistB.append(os.path.join(path, file)) if not file.isdigit() and os.path.isdir(os.path.join(path, file)): filelistC = os.listdir(os.path.join(path, file)) for nestedfile in filelistC: if not nestedfile.isdigit() and not os.path.isdir(os.path.join(path, file, nestedfile)): filelistB.append(os.path.join(path, file, nestedfile)) finishTime = time.time() - startTime print 'total run time: %s' % finishTime return filelistB # ------------- End function # # get the last modification time in epoch and filesize # def getFileMtimeStat(filepath): if os.path.isfile(filepath): filemtime = time.localtime(os.stat(filepath).st_mtime) filesize = os.stat(filepath).st_size return filepath, filemtime, filesize # ------------- End function if options.path: files = genFileList(options.path) fileInfo = [getFileMtimeStat(file) for file in files] fileInfo.sort() for line in fileInfo: print line print len(fileInfo)
以上是关于以字节为单位获取文件mod time和size(python)的主要内容,如果未能解决你的问题,请参考以下文章
linux里文件以B还是KB还是什麽算大小的 postfix帮看一下
如何发现 std::vector 的大小/长度(以字节为单位)?
汇编 x86 从任何文件读取并转义所有特殊字符并以字节为单位获取文件大小
C++ std::string::size()函数(返回字符串的长度,以字节为单位)(与std::string::length()函数相同)