Advertisement
Guest User

Untitled

a guest
May 1st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. from sys import argv
  2. import os
  3. import file_class
  4.  
  5. def main():
  6.  
  7.     if len(argv) <= 1 or os.path.exists(argv[1]) == False:
  8.         usage()
  9.  
  10.     # If the path exists and is specified, variables are initialized and packed into a list for easier passing to/from traversal function
  11.     path = argv[1]
  12.     userPath = os.path.normpath(path)
  13.     pyFilesList = [ ]
  14.     initialFiles = 0
  15.    
  16.     print("\n"+ "Original Path is Python file: "+ str(pyCheck(path)) + "\n")
  17.  
  18.     finalData = dirTraversal(userPath, pyFilesList, initialFiles)
  19.  
  20.     # Unpacks return tuple to separate Python file instance list from total count of files
  21.     finalPyList = finalData[0]
  22.     finalTotalFiles = finalData[1]
  23.     finalPyCount = 0
  24.     finalLineCount = 0
  25.  
  26.     # Computes and outputs total counts and average number of lines
  27.     for instance in finalPyList:
  28.         finalPyCount += 1
  29.         finalLineCount += instance.getLines()
  30.  
  31.     print("Final Total File Count: "+ str(finalTotalFiles))
  32.  
  33.     print("Final Python File Count: "+ str(finalPyCount))
  34.  
  35.     print("Final Python File Line Count: "+ str(finalLineCount))
  36.  
  37.     print("Average Number of Lines per Python file: " + str(finalLineCount / finalPyCount))
  38.  
  39.    
  40.  
  41.  
  42.  
  43. def dirTraversal(anyPath, pyFilesList, totalFiles):
  44.  
  45.     if os.path.isfile(anyPath):
  46.         # Terminating condition for Python files
  47.         if pyCheck(anyPath) == True:
  48.             newPyFile = file_class.File()
  49.             newPyFile.setPath(anyPath)
  50.             newPyFile.setName(os.path.basename(anyPath))
  51.             newPyFile.setLines(countLines(anyPath))
  52.             pyFilesList.append(newPyFile)
  53.             totalFiles += 1
  54.             return pyFilesList, totalFiles
  55.  
  56.         # Terminating condition for non-Python files
  57.         else:
  58.             totalFiles += 1
  59.             return pyFilesList, totalFiles
  60.  
  61.     # Handles non-file cases
  62.     else:
  63.         files = os.listdir(anyPath)
  64.         origPath = anyPath
  65.         for x in range(len(files)):
  66.             newPath = str(origPath) + "/" + str(files[x])
  67.             subfolderData = dirTraversal(newPath, pyFilesList, totalFiles)
  68.             totalFiles = subfolderData[1]
  69.         return pyFilesList, totalFiles
  70.        
  71. # Checks to see if the given path is a Python file
  72. def pyCheck(path):
  73.  
  74.     if path.endswith(".py"):
  75.         return True
  76.     else:
  77.         return False
  78.  
  79. # Counts the number of lines in a given Python file
  80. def countLines(path):
  81.  
  82.     lineCount = 0
  83.  
  84.     countFile = open(path, "r")
  85.  
  86.     for line in countFile:
  87.         lineCount += 1
  88.  
  89.     countFile.close()
  90.  
  91.     return lineCount
  92.  
  93.  
  94.  
  95. def usage():
  96.     print("Usage: python3 recursive_dir_traversal.py 'path/to/file/written/like/this/quotes/included' ")
  97.     exit(0)
  98.    
  99.  
  100. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement