-
Notifications
You must be signed in to change notification settings - Fork 0
/
walkfiles.py
62 lines (52 loc) · 1.6 KB
/
walkfiles.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
import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
key = input('The key of file:\n')
count = 0
def format_size(sizeFile):
base = 1024
kilo = base
mega = base ** 2
giga = base ** 3
tera = base ** 4
peta = base ** 5
if sizeFile < base:
sizeFile = base
text = 'B'
elif sizeFile < kilo:
sizeFile /= kilo
text = 'K'
elif sizeFile < mega:
sizeFile /= mega
text = 'M'
elif sizeFile < giga:
sizeFile /= giga
text = 'G'
elif sizeFile < tera:
sizeFile /= tera
text = 'T'
else:
sizeFile /= peta
text = 'P'
sizeFile = round(sizeFile, 2)
return f'{sizeFile}{text}'
for root, directory, files in os.walk(ROOT_DIR):
for file in files:
if key in file:
try:
ROOT = os.path.join(root, file)
name_File, ext_File = os.path.splitext(file)
sizeFile = os.path.getsize(ROOT)
print(f'File: {file}')
print(f'Path: {ROOT}')
print(f'Name File: {name_File}')
print(f'Ext: {ext_File}')
print(f'Size: {sizeFile}')
print(f'Formatted Size:', format_size(sizeFile))
count += 1
except PermissionError as error:
print('Not permissions!')
except FileNotFoundError as error:
print('File not found!')
except Exception as error:
print(error)
print(f'Number of files: {count}')