Advertisement
Guest User

wykop_wyzwaniepython_zadanie2_łatwe

a guest
Jul 27th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. import os
  2. import sys
  3.  
  4. class FileStatItem:
  5.     def __init__(self,size):
  6.         self.size = size
  7.         self.count = 1
  8.  
  9.     def addFile(self,size):
  10.         self.size+=size
  11.         self.count+=1
  12.  
  13. def countFiles(fileStatistics, filesCounter, directory):
  14.     filesList = os.listdir(directory)
  15.     for file in filesList:
  16.         path = os.path.abspath("{}\{}".format(directory, file))
  17.         if os.path.isfile(path):
  18.             filesCounter+=1
  19.             size = os.path.getsize(path)
  20.            
  21.             splitFileName = list(map(lambda x: x[::-1].lower(), file[::-1].split(".", 1)[::-1]))
  22.             if len(splitFileName) == 2:
  23.                 filename, extension = splitFileName
  24.             else:
  25.                 filename, extension = file,""
  26.                
  27.             if extension in fileStatistics.keys():
  28.                 fileStatistics[extension].addFile(size)
  29.             else:
  30.                 fileStatistics[extension] = FileStatItem(size)
  31.         if os.path.isdir(path):
  32.             fileStatistics, filesCounter = countFiles(fileStatistics, filesCounter,path)
  33.  
  34.     return fileStatistics, filesCounter
  35.  
  36. def main(argv):
  37.     currentDir = os.path.abspath(os.path.curdir)
  38.     if(os.path.exists(argv[0])):
  39.         os.chdir(argv[0])
  40.  
  41.     fileStatistics = {}
  42.     filesCounter = 0
  43.  
  44.     directory = os.path.abspath(os.path.curdir)
  45.     fileStatistics, filesCounter = countFiles(fileStatistics, filesCounter, directory)
  46.  
  47.     os.chdir(currentDir)
  48.     with open(argv[1], mode='w') as file:
  49.         for key, value in sorted(fileStatistics.items()):
  50.             noOfHash = round(50 * value.count / filesCounter)
  51.             noOfSpaces = 60 - noOfHash
  52.             file.write("{:>5}{:14}B{}{}\n".format(key, value.size, " " * noOfSpaces, "#" * noOfHash))
  53.  
  54. if __name__ == "__main__":
  55.     main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement