30.12.2017 г.

Calculate size of folder with python scripts only folder not RAR

#Import module
import os
import sys

class InFolder:

    def __init__(self, folder):
        self.folder = folder

    def search(self):
        founded = set()
        for root, dirname, filename in os.walk(self.folder):
            clean_root = os.path.normpath(root)
            clear_root_name = os.path.normcase(clean_root)
            founded.add(clear_root_name)
        founded.remove(self.folder)
        return founded


if len(sys.argv) == 1:
        print("Try again with add path to scan!")
        sys.exit('Usage: python %s path-name' % sys.argv[0])
elif not os.path.exists(sys.argv[1]):
    print("Try again with add path to scan!")
    sys.exit('Usage: python %s and corect file name (example c:\\)' % sys.argv[0])
    sys.exit()
else:
    my_project = InFolder(sys.argv[1])
    clear_folder = my_project.search()


class Second:

    def __init__(self, path2):
        self.path2 = path2

    def calculate_size(self):
        my_calc = dict()
        size = 0
        for root, dirname, filename in os.walk(self.path2):
            for file in filename:
                f_path = os.path.join(root, file)
                size += os.path.getsize(f_path)
            my_calc[self.path2] = (size/1024)/1024
        return my_calc


total = dict()

for f in clear_folder:
    main = Second(f)
    for i, v in main.calculate_size().items():
        total[i] = v

for line in sorted(total.items(), key=lambda x: x[1]):
    print('{0:>10} - {1:.2f}Mb'.format(*line))

16.12.2017 г.

Read excell file with python, validate and return defaultdict

import os
import xlrd
from collections import defaultdict
import sys
from pprint import pprint
import re

'''
Some name1     2345        someemal1@gmail.com
Some name12    23456        someemal2@gmail.com
Some name1    0            someemal1@gmail.com
Some name3    0889345        someemal1@gmail.com
'''

filename = "somefile.xls"
result = defaultdict(list)


def checkFilename(filename):
    '''Check file is excel or not '''
    if filename.endswith('xls'):
        return filename
    else:
        return 'Not valid filename'
        sys.exit()


class ReadXLS:

    def __init__(self, checkFilename):
        self.filename = checkFilename

    def realReadFilename(self):
        '''Read xls file with xlrd module '''
        try:
            workbook = xlrd.open_workbook(self.filename)
        except:
            print("some error...")
            sys.exit()
        else:
            try:
                sheet = workbook.sheet_by_index(0)
            except IndexError as ie:
                print(ie)
                sys.exit()
            else:
                for rowx in range(sheet.nrows):
                    columnmapping = sheet.row_values(rowx)
                    yield columnmapping

    def checkResult(self):
        '''add to result only what i whant'''
        for line in ReadXLS.realReadFilename(self):
            if ((line[0] in (None, '')) or (line[1] in (None, ''))
                    or (line[2] in (None, '')) or not
                    re.search(r'[\w.-]+@[\w.-]+.\w+', line[2])):
                continue
            else:
                #result[line[2].upper()].append((line[0], line[1]))
                result[line[2].upper()].append(
                    (line[1].strip().replace(' ', ''), line[0].strip())
                )

    def __repr__(self):
        ''' Why I can '''
        return "{}".format(self.filename)


if __name__ == '__main__':
    test = ReadXLS(checkFilename(filename))
    test.checkResult()
    pprint(result)