Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- class FileStatItem:
- def __init__(self,size):
- self.size = size
- self.count = 1
- def addFile(self,size):
- self.size+=size
- self.count+=1
- def countFiles(fileStatistics, filesCounter, directory):
- filesList = os.listdir(directory)
- for file in filesList:
- path = os.path.abspath("{}\{}".format(directory, file))
- if os.path.isfile(path):
- filesCounter+=1
- size = os.path.getsize(path)
- splitFileName = list(map(lambda x: x[::-1].lower(), file[::-1].split(".", 1)[::-1]))
- if len(splitFileName) == 2:
- filename, extension = splitFileName
- else:
- filename, extension = file,""
- if extension in fileStatistics.keys():
- fileStatistics[extension].addFile(size)
- else:
- fileStatistics[extension] = FileStatItem(size)
- if os.path.isdir(path):
- fileStatistics, filesCounter = countFiles(fileStatistics, filesCounter,path)
- return fileStatistics, filesCounter
- def main(argv):
- currentDir = os.path.abspath(os.path.curdir)
- if(os.path.exists(argv[0])):
- os.chdir(argv[0])
- fileStatistics = {}
- filesCounter = 0
- directory = os.path.abspath(os.path.curdir)
- fileStatistics, filesCounter = countFiles(fileStatistics, filesCounter, directory)
- os.chdir(currentDir)
- with open(argv[1], mode='w') as file:
- for key, value in sorted(fileStatistics.items()):
- noOfHash = round(50 * value.count / filesCounter)
- noOfSpaces = 60 - noOfHash
- file.write("{:>5}{:14}B{}{}\n".format(key, value.size, " " * noOfSpaces, "#" * noOfHash))
- if __name__ == "__main__":
- main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement