Completed
Pull Request — master (#37)
by
unknown
28s
created

get_file_size()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
1
# -*- coding: utf8 -*-
2
import os
3
import datetime
4
import math
5
6
7
def get_last_modified_date(filename):
8
    """
9
    Get the last modified date of a given file
10
11
    :param filename: string: pathname of a file
12
    :return: Date
13
    """
14
    t = os.path.getmtime(filename)
15
    return datetime.date.fromtimestamp(t).strftime('%d/%m/%Y')
16
17
18
def get_file_size(filename):
19
    """
20
    Get the file size of a given file
21
22
    :param filename: string: pathname of a file
23
    :return: human readable filesize
24
    """
25
    return convert_size(os.path.getsize(filename))
26
27
28
def convert_size(size_bytes):
29
    """
30
    Transform bytesize to a human readable filesize
31
32
    :param size_bytes: bytesize
33
    :return: human readable filesize
34
    """
35
    if size_bytes == 0:
36
        return "0B"
37
    size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
38
    i = int(math.floor(math.log(size_bytes, 1024)))
39
    p = math.pow(1024, i)
40
    s = round(size_bytes / p, 2)
41
    return "%s %s" % (s, size_name[i])
42