Posted By

mattsn0w on 09/30/10


Tagged

file attributes osstat


Versions (?)

get file mod time and size in bytes (python)


 / Published in: Python
 

  1. #!/bin/env python
  2. import os, time, re, optparse
  3.  
  4.  
  5. usage = '''
  6.  
  7. '''
  8.  
  9. parser = optparse.OptionParser(usage=usage, version='%prog v0.2')
  10. parser.add_option('--path', dest='path', help='provided path to log dir.')
  11. #parser.add_option('--', dest='', help='')
  12.  
  13. (options, args) = parser.parse_args()
  14.  
  15. #
  16. # This function takes a top level path as argument then generates a file list with up to 1 deep nested directories.
  17. # Filenames containing only numbers are not added to this list
  18. # questions:
  19. # Will sorting lists prior to for loop make it run faster? initial time to scan buildbot logdirs: 23 minutes
  20. #
  21. def genFileList(path):
  22. startTime = time.time()
  23. filelistA = os.listdir(path)
  24. filelistB = list()
  25. filelistC = list()
  26. for file in filelistA:
  27. if not file.isdigit() and not os.path.isdir(os.path.join(path, file)):
  28. filelistB.append(os.path.join(path, file))
  29. if not file.isdigit() and os.path.isdir(os.path.join(path, file)):
  30. filelistC = os.listdir(os.path.join(path, file))
  31. for nestedfile in filelistC:
  32. if not nestedfile.isdigit() and not os.path.isdir(os.path.join(path, file, nestedfile)):
  33. filelistB.append(os.path.join(path, file, nestedfile))
  34. finishTime = time.time() - startTime
  35. print 'total run time: %s' % finishTime
  36. return filelistB
  37. # ------------- End function
  38.  
  39. #
  40. # get the last modification time in epoch and filesize
  41. #
  42. def getFileMtimeStat(filepath):
  43. if os.path.isfile(filepath):
  44. filemtime = time.localtime(os.stat(filepath).st_mtime)
  45. filesize = os.stat(filepath).st_size
  46. return filepath, filemtime, filesize
  47. # ------------- End function
  48.  
  49. if options.path:
  50. files = genFileList(options.path)
  51. fileInfo = [getFileMtimeStat(file) for file in files]
  52. fileInfo.sort()
  53.  
  54. for line in fileInfo:
  55. print line
  56. print len(fileInfo)

Report this snippet  

You need to login to post a comment.