def print_result(files: dict):
    total_size = 0
    for key, value in files.items():
        print("[ {:>10} ] {}".format(humansize.approximate_size(value), key))
        total_size += value
    print("==========================================================================================")
    print("[ {:>10} ] Total size".format(humansize.approximate_size(total_size)))
def main():
    # Set up the regexes
    re_filename = re.compile(r".*Purged '([^']+)'")
    re_bytes = re.compile(r".*size=(\d+)")
    re_atime = re.compile(r".*last access (\d+\.\d+)")

    # command-line args
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', help='verbose output', action='store_true')
    parser.add_argument('-a', '--atime_threshold', default=30.0, type=float)
    parser.add_argument('-m', '--max_records', default=None, type=int)
    parser.add_argument('log_file')
    args = parser.parse_args()

    # report variables
    total_bytes = 0
    bad_log_lines = 0
    total_files = 0
    record_count = 0

    # Default input file factory assumes a text file
    input_file_factory = lambda f: open(f)

     # If we have a gzip file, log it and replace the factory
    _, ext = os.path.splitext(args.log_file)
    if ext == '.gz':
        print('Using gzip file open factory ...')
        input_file_factory = lambda f: gzip.open(f, mode='rt')

    # parse the log line by line
    with input_file_factory(args.log_file) as f:
        for line in f:
            record_count += 1
            if args.max_records and record_count > args.max_records:
                break
            try:
                size_bytes = (int)(re_bytes.match(line).group(1))
                atime = (float)(re_atime.match(line).group(1))

                if atime >= args.atime_threshold:
                    total_bytes += size_bytes
                    total_files += 1

            except:
                if args.verbose:
                    e = sys.exc_info()[0]
                    print('Error parsing line {0}:\n\t{1}'.format(line, e))

                bad_log_lines += 1
                continue

    # Final report
    if args.verbose:
        print('Source log file: {0}'.format(args.log_file))
    if bad_log_lines > 0:
        print('Bad log lines (skipped): {0}'.format(bad_log_lines))
    print('Threshold: {0} days'.format(args.atime_threshold))
    print('File count: {0}'.format(total_files))
    print('Total freed: {0}'.format(approximate_size(total_bytes)))
Exemple #3
0
def getimageinfo(image_path):
	images = []
	image_files = glob('%s/image_*.jpg'%image_path)

	for i_file in image_files:
		i_file_name	= path.basename(i_file)
		i_file_id	= i_file_name.split('.')[0]	# Remove path and extension
		i_file_dt	= getdatetime(i_file_id.split('_')[2], i_file_id.split('_')[3])
		images.append({'id': i_file_id, 'size': approximate_size(path.getsize(i_file), False), 'dt': i_file_dt, 'fname': "%s/%s"%(image_path,i_file_name)})
	return images
Exemple #4
0
def getvideoinfo(video_path):
	videos = []
	video_files = glob('%s/video_*.mp4'%video_path)

	for v_file in video_files:
		v_file_name	= path.basename(v_file)
		v_file_id	= v_file_name.split('.')[0]	# Remove path and extension
		v_file_dt	= getdatetime(v_file_id.split('_')[2], v_file_id.split('_')[3])
		
		videos.append({'id': v_file_id, 'size': approximate_size(path.getsize(v_file), False), 'dt': v_file_dt, 'fname': "%s/%s"%(video_path,v_file_name)})
	return videos
Exemple #5
0
def approximate_size(bytes, k_is_1024=False):
    '''Convert a file size to human-readable form.

    Keyword arguments:
    size -- file size in bytes
    k_is_1024_bytes -- if True,  use multiples of 1024
                                if False(default), use multiples of 1000

    Returns: string

    '''
    return humansize.approximate_size(bytes, k_is_1024)
Exemple #6
0
def listdir(dir):
    cmd = 'ls -l ' + dir
    print "Command to run:", cmd  ## good to debug cmd before actually running it
    (status, output) = commands.getstatusoutput(cmd)
    if status:  ## Error case, print the command's output to stderr and exit
        sys.stderr.write(output)
        sys.exit(status)
    print output  ## Otherwise do something with the command's output
    cmd = 'cat foo.txt'
    os.system(cmd)
    print(os.getcwd())
    os.chdir('..')
    print(os.getcwd())
    os.chdir('python')
    listoffiles = glob.glob('*.py')
    print listoffiles
    metadata = os.stat(listoffiles[0])
    print metadata.st_mtime
    print metadata.st_size
    print time.localtime(metadata.st_mtime)
    print humansize.approximate_size(metadata.st_size)
    print[os.path.realpath(f) for f in glob.glob('*.py')]
    print[f for f in glob.glob('*.py') if os.stat(f).st_size > 2000]
    print[(os.stat(f).st_size, os.path.realpath(f)) for f in glob.glob('*.py')]
example_directory = os.path.join(os.path.dirname(current_dir), 'example')
#print(os.path.join(os.path.expanduser('~'), 'code', 'python3-study', 'dive-into-python3', 'example', 'humansize.py'))

#get path of a file from the sibling directory
humansize_file = os.path.join(example_directory, 'humansize.py')
'''
Following is an example of a multi-variable assignment.
Here os.path.split(pathname) is returning a tuple of two variables
(containing directory and file name) if the path of a file name
is given to the function. Each variable receives the value of
the corresponding element of the returned tuple.
'''
(dirname, filename) = os.path.split(humansize_file)
print(dirname, ', ', filename)
(shortname, extension) = os.path.splitext(filename)
print(shortname, ', ', extension)
'''
os.path also contains the os.path.splitext() function, which splits a filename and returns a tuple
containing the filename and the file extension. You use the same technique to assign each of them to
separate variables.
'''
print('-------Printing Metadata--------')

metadata = os.stat(humansize_file)
print(metadata.st_mtime)
print(time.localtime(metadata.st_mtime))
print(metadata.st_size)
print(humansize.approximate_size(metadata.st_size))
print(humansize.approximate_size(
    os.stat(__file__).st_size))  # current file size
Exemple #8
0
def test_4000():
    assert approximate_size(4000, a_kilobyte_is_1024_bytes=False) == "4.0 KB"
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' a test module '

__author__ = 'dreamapple'

from humansize import approximate_size

# 测试传递参数的顺序
print(approximate_size(100000, True))
print(approximate_size(size=100000, a_kilobyte_is_1024_bytes=False))
print(approximate_size(a_kilobyte_is_1024_bytes=True, size=1000000))

print(approximate_size.__doc__)

# 使用 try...except 块来处理异常
try:
    not_define_var
except NameError:
    not_define_var = 1

print(not_define_var)
Exemple #10
0
def test_4000_case11():
    assert approximate_size(4000, a_kilobyte_is_1024_bytes=True) == "3.9 KiB"
Exemple #11
0
from humansize import approximate_size
approximate_size(4000, a_kilobyte_is_1024_bytes=False)
approximate_size(size=4000, a_kilobyte_is_1024_bytes=False)
approximate_size(a_kilobyte_is_1024_bytes=False, 4000)
approximate_size(size=4000, False)
def test_4000_positional_args():
    assert approximate_size(4000, a_kilobyte_is_1024_bytes=False) == "4.0 KB"
Exemple #13
0
def test_1024():
    assert approximate_size(1000000000000) == "931.3 GiB"
def test_4000_keyword_args():
    assert approximate_size(size=4000,
                            a_kilobyte_is_1024_bytes=False) == "4.0 KB"
def test_4KB_reverse():
    assert approximate_size(a_kilobyte_is_1024_bytes=False,
                            size=4000) == "4.0 KB"
Exemple #16
0
print("type(metadata_dict) = {0}".format(type(metadata_dict)))

print("list(metadata_dict.keys()) = {0}".format(list(metadata_dict.keys())))

print("metadata_dict['ch03sc04.py'].st_size = {0}".format(
    metadata_dict['ch03sc04.py'].st_size))

print("metadata_dict = {f:os.stat(f) for f in glob.glob('*')}")
metadata_dict = {f:os.stat(f) for f in glob.glob('*')}

print("program_dict = \
{os.path.splitext(f)[0]:humansize.approximate_size(meta.st_size) \
for f, meta in metadata_dict.items() if meta.st_size > 1000}")

program_dict = {
        os.path.splitext(f)[0]:humansize.approximate_size(meta.st_size)
        for f, meta in metadata_dict.items() if meta.st_size > 1000}

print("list(humansize_dict.keys()) = {0}".format(list(program_dict.keys())))

print("humansize_dict['ch03sc04'] = {0}".format(
    program_dict['ch03sc04']))

print("\n########################################################################")
print("##### 3.4.1 - Other Fun Stuff To Do With Dictionary Comprehensions #####")
print("########################################################################\n")

print("a_dict = {'a': 1, 'b': 2, 'c': 3}")
a_dict = {'a': 1, 'b': 2, 'c': 3}

value:key for key, value in a_dict.items()
Exemple #17
0
a_list = [1, 9, 8, 4]
b_list = [elem * 2 for elem in a_list]
print(a_list)
print(b_list)
import os
import glob
files = [os.path.realpath(f) for f in glob.glob('../*.txt')]
print(files)
#Create list containing all files found in '../' larger than 6000 bytes
big_files = [f for f in glob.glob('../*') if os.stat(f).st_size > 6000]
print(big_files)
import humansize
print([(humansize.approximate_size(os.stat(f).st_size), f)
       for f in glob.glob('../*.txt')])
a_dict = {'a': 1, 'b': 2, 'c': 3}
print({value: key for key, value in a_dict.items()})
a_set = set(range(10))
print(a_set)
b_set = {x**2 for x in a_set}
print(b_set)
b_set = {x for x in a_set if x % 2 == 0}
print(b_set)
b_set = {2**x for x in a_set}
print(b_set)
# generatorova notace slovniku
print('GENERATOROVA NOTACE SLOVNIKU')
metadata = [(f, os.stat(f)) for f in glob.glob('*test*.py')
            ]  # chceme nazev souboru a jeho metadata
print(metadata[0])
metadata_dict = {f: os.stat(f) for f in glob.glob('*test*.py')}
print(type(metadata_dict))
print(list(metadata_dict.keys()))  # klicem jsou jmena souboru
print(metadata_dict['alphameticstest.py'].st_size)

metadata_dict = {f: os.stat(f)
                 for f in glob.glob('*')
                 }  # file a metadata pro vsechny soubory z current working dir
humansize_dict = {
    os.path.splitext(f)[0]: humansize.approximate_size(meta.st_size)
    for f, meta in metadata_dict.items() if meta.st_size > 6000
}
# nazev souboru bez pripony jako klic, velikost jako hodnota for file a metadata object in items() if velikost > 6000
print(list(humansize_dict.keys()))  # je jich 6 jako vyse
print(humansize_dict['romantest9']
      )  # vrati hodnotu velikosti zpracovanou fci approximate_size

# zamena klicu a hodnot ve slovniku
a_dict = {'a': 1, 'b': 2, 'c': 3}
print({value: key for key, value in a_dict.items()})
# jde to ale delatjen u immutable data types
a_dict = {'a': [1, 2, 3], 'b': 4, 'c': 5}
# print({value:key for key, value in a_dict.items()}) vyvola TypeError unhashable type list

# generatorova notace mnozin
Exemple #19
0
def approximate_size(size, a_kilobyte_is_1024_bytes=False) :
    "return human readable size, default to kilo = 1000"
    return humansize.approximate_size(size = size, a_kilobyte_is_1024_bytes = a_kilobyte_is_1024_bytes)
Exemple #20
0
def test_4000_order():
    assert approximate_size(a_kilobyte_is_1024_bytes=False,
                            size=4000) == "4.0 KB"
Exemple #21
0
txt_file_path_list = [os.path.realpath(f) for f in glob.glob('*.txt')]
print(txt_file_path_list)

# You can even add a condition into the list
txt_files_list_under_6000 = [
    f for f in glob.glob('*.txt') if os.stat(f).st_size < 6000
]
print(txt_file_path_list)

# Creating tuples of file size and path and packing it into a list
file_sizes_and_paths = [(os.stat(f).st_size, os.path.realpath(f))
                        for f in glob.glob('*.txt')]
print(file_sizes_and_paths)

# Using external function in the list
just_another_list = [(humansize.approximate_size(os.stat(f).st_size),
                      os.path.realpath(f)) for f in glob.glob('*.txt')]
print(just_another_list)

# Dictionary Comprehensions
metadata = [(f, os.stat(f)) for f in glob.glob('*.txt')]
print()
print(metadata)
metadata_dict = {f: os.stat(f) for f in glob.glob('*.txt')}
print(metadata_dict)
print(metadata_dict.keys())
print()
print(metadata_dict['kappa.txt'])

# Dictionary Comprehensions with conditions
Exemple #22
0
def test_1000():
    assert approximate_size(1000000000000, False) == "1.0 TB"
def test_4000_keyword_args_out_of_order():
    assert approximate_size(a_kilobyte_is_1024_bytes=False,
                            size=4000) == "4.0 KB"
Exemple #24
0
def approximate_size(size):
    """Calls humansize approximate size with a default parameter of false"""
    return humansize.approximate_size(size, a_kilobyte_is_1024_bytes=False)
Exemple #25
0
pathname = '/Users/pilgrim/diveintopython3/examples/humansize.py'
(dirname, filename) = os.path.split(pathname)
print(dirname)
print(filename)

(shortname, extension) = os.path.splitext(filename)
print(shortname)
print(extension)


desktop = "C:/Users/ia.kudryashov/Desktop/"
print(glob.glob(desktop+'*66.xlsx'))

metadata = os.stat(desktop+'Rosgeo_FAS8200_85966.xlsx')
print(metadata.st_mtime)
print(time.localtime(metadata.st_mtime))
print(metadata.st_size)

os.chdir('C:\\Users\\ia.kudryashov\\Desktop\\python')
import sys
sys.path.insert(0,'C:\\Users\\ia.kudryashov\\Desktop\\python')
import humansize
print(humansize.approximate_size(metadata.st_size))

print(os.path.realpath('humansize.py'))

os.chdir('C:\\Users\\ia.kudryashov\\Desktop')
print(glob.glob('*.xlsx'))
print([os.path.realpath(f) for f in glob.glob('*.xlsx')])
print([(humansize.approximate_size(os.stat(f).st_size), f) for f in glob.glob('*.xlsx')])
a_list = [1, 9, 8, 4]
b_list = [elem * 2 for elem in a_list]
print(a_list)
print(b_list)
import os
import glob
files = [os.path.realpath(f) for f in glob.glob('../*.txt')]
print(files)
#Create list containing all files found in '../' larger than 6000 bytes
big_files = [f for f in glob.glob('../*') if os.stat(f).st_size > 6000]
print(big_files)
import humansize
print([(humansize.approximate_size(os.stat(f).st_size), f) for f in glob.glob('../*.txt')])
a_dict = {'a': 1, 'b': 2, 'c': 3}
print({value:key for key, value in a_dict.items()})
a_set = set(range(10))
print(a_set)
b_set = {x**2 for x in a_set}
print(b_set)
b_set = {x for x in a_set if x % 2 == 0}
print(b_set)
b_set = {2**x for x in a_set}
print(b_set)
Exemple #27
0
print(os.getcwd())
pathname = '/home/deen/Project/Python/Dive into Python 3/humansize.py'
print(os.path.split(pathname))
(dirname, filename) = os.path.split(pathname)
print(dirname, filename)
(name, ext) = os.path.splitext(filename)
print(name, ext)

#globbbing
print(glob.glob('*')) #The glob module takes a wildcard and returns the path of all files and directories matching the wildcard
print(glob.glob('*.zip'))

#getting metadata
metadata = os.stat('open_gapps-arm-6.0-pico-20160519.zip')
print(metadata)
time = time.localtime(metadata.st_mtime)
print(time.tm_mday,'/', time.tm_mon, '/', time.tm_year)
print(metadata)
print(metadata.st_size)
print(humansize.approximate_size(metadata.st_size, a_kilobyte_is_1024_bytes = False))

#real path
print(os.path.realpath('open_gapps-arm-6.0-pico-20160519.zip'))

#putting globbed paths in a list

a_list = [os.path.realpath(f) for f in glob.glob('*.zip')]
print(a_list)

print([os.path.realpath(f) for f in glob.glob('*.zip') if os.stat(f).st_size > 6000])
Exemple #28
0
from humansize import approximate_size

print(approximate_size(1000000000000, False))
Exemple #29
0
""" humansize.py:
# $ python3 humansize.py
# 1.0 TB
# 931.3 GiB
"""
""" import:

¿Dónde python busca el código?
¿Dónde ipython busca el código?
¿Cómo se importa código?
"""

import sys
sys.path.insert(0, "/Users/javier/git/hashcode-training/ejemplos/sesion-2")
from humansize import approximate_size
approximate_size(1000000000000)
""" definición de una función:

def approximate_size(size, a_kilobyte_is_1024_bytes=True):
    pass
"""

approximate_size(4000, False)
approximate_size(4000, a_kilobyte_is_1024_bytes=False)
approximate_size(size=4000, a_kilobyte_is_1024_bytes=False)
approximate_size(a_kilobyte_is_1024_bytes=False, size=4000)
# approximate_size(a_kilobyte_is_1024_bytes=False, 4000)
# approximate_size(size=4000, False)
""" docstrings:

"""
Exemple #30
0
# Filename: humansize_demo.py

import humansize
import sys

si_suffixes = humansize.SUFFIXES[1000]
print(si_suffixes)
print('1000{0[0]} = 1{0[1]}'.format(si_suffixes))
print('1024{0[0]} = 1{0[1]}'.format(humansize.SUFFIXES[1024]))

print('1MB = 1000{0.modules[humansize].SUFFIXES[1000][0]}'.format(sys))

print(humansize.approximate_size(1024, True))
print(humansize.approximate_size(1000, False))
Exemple #31
0
import humansize
print (humansize.approximate_size(1000000, True))
print(humansize.approximate_size.__doc__)
Exemple #32
0
import os
print(os.getcwd())
#...\diveintopython3
os.chdir('../')
print(os.getcwd())
#...\Desktop
print(os.path.join('/Users/Michael/Desktop/diveintopython3/', 'filesystem.py'))
print(os.path.join('/Users/Michael/Desktop/diveintopython3', 'filesystem.py'))
print(os.path.expanduser('~'))
pathname = '/Users/Michael/Desktop/diveintopython3/filesystem.py'
print(os.path.split(pathname))
(dirname, filename) = os.path.split(pathname)
print(dirname)
(shortname, extension) = os.path.splitext(filename)
print(extension)
import glob
print(glob.glob('*.txt'))
metadata = os.stat('finances.txt')
print(metadata.st_mtime)
import time
print(time.localtime(metadata.st_mtime))
print(metadata.st_size)
import humansize
print(humansize.approximate_size(metadata.st_size))
print(os.path.realpath('finances.txt'))
Exemple #33
0
from humansize import approximate_size
import os
import glob

if __name__ == '__main__':
    sizeOf1000 = approximate_size(49595949393, False)
    sizeOf1024 = approximate_size(49595949393)
    print(sizeOf1000)
    print(sizeOf1024)
    x = 1
    print(x + 1)
    print(True)
    print(False)
    print(type(1))
    print(isinstance(1, int))
    print(isinstance(1.0, float))
    print(isinstance(float(1), float))

    a_list = ['a', 'b', 'c']
    a_list.extend(['d', 'e', 'f'])
    print(a_list)
    a_list.append(['g', 'h', 'i'])
    print(a_list)
    print(len(a_list))
    print('a' in a_list)
    print('----')
    # print(a_list.count('b'))
    # print(a_list.index('x'))
    print(a_list[1])
    print(a_list)
    del a_list[1:2]
Exemple #34
0
# -*- coding: utf8 -*-
'''
Created on 2014年5月15日

@author: EASON
'''

import humansize

if __name__ == '__main__':
    print('hello')
    print(humansize.approximate_size(4096, True))
Exemple #35
0
'''
Created on Sep 19, 2017

@author: louie
'''

import humansize

if __name__ == '__main__':
    print(humansize.approximate_size(1000000000000, False))
    print(humansize.approximate_size(1000000000000))