Return to Snippet

Revision: 32615
at January 22, 2011 06:30 by mattsn0w


Updated Code
#!/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)

Revision: 32614
at September 30, 2010 10:07 by mattsn0w


Updated Code
#!/bin/env python
import os

def getFileMtimeStat(path, file):
    filename  = os.path.join(path, file)
    if filename.isdigit():
        pass
    if os.path.isfile(filename):
        filemtime  = os.stat(filename).st_mtime
        filesize   = os.stat(filename).st_size
    return filemtime, filesize

Revision: 32613
at September 30, 2010 10:06 by mattsn0w


Initial Code
#!/bin/env
import os

def getFileMtimeStat(path, file):
    filename  = os.path.join(path, file)
    if filename.isdigit():
        pass
    if os.path.isfile(filename):
        filemtime  = os.stat(filename).st_mtime
        filesize   = os.stat(filename).st_size
    return filemtime, filesize

Initial URL

                                

Initial Description

                                

Initial Title
get file mod time and size in bytes (python)

Initial Tags
file

Initial Language
Python