-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathculprits.py
executable file
·71 lines (65 loc) · 2.08 KB
/
culprits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#################################################################
#GPL GNU License
#@Author: David Okwii
#@Email: [email protected]
#@date: 2012
#@description: This python cross-platform script sorts files in a
#given directory in descending order.
#Feel free to modify it so that it do also sort directories in
#descending order instead of just files
#################################################################
import os
import itertools
#get the size of the file
def get_size(start_path = '.'):
total_size = 0
files = {}
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
if os.path.exists(fp):
file_size = os.path.getsize(fp)
files[file_size] = fp
print fp
total_size += file_size
return total_size, files
#format file size into human readable format!
def format_size(size):
if size < 1024:
#format to bytes
size = "%dB" %(size)
return size
if size >=1024 and size <= 1024*1024:
#format size to KB
size = size / 1024
return "%dKB"% size
elif size >= 1024*1024 and size <= 1024*1024*1024:
#format size to MB
size = size / (1024*1024)
return "%dMB" % size
#and size <= 1024*1024*1024*1024
elif size >= 1024*1024*1024:
#format size to GB
size = size / (1024*1024*1024)
return "%dGB" % size
print "pliz type exit to exit prog at any time"
while(True):
start_path = raw_input("pliz enter your path >>> ")
n = input("pliz enter no of files for the biggest files you want to see >>>")
if start_path != "exit":
total_size, files = get_size(start_path)
#sort the file sizes from biggest to smallest
file_sizes = sorted(files, reverse=True)
#get only the first n elements of list
print "\n\n ###THESE ARE THE BIGGEST %d FILES ### \n" % n
for file_size in file_sizes[:n]:
file_path = files[file_size]
file_size = format_size(file_size)
print "%s: %s" %(file_size, file_path)
print "\n###################################"
else:
selection = raw_input("do you really want to quit?(yes/no)")
if selection == "no":
continue
else:
break