-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
46 lines (35 loc) · 1.02 KB
/
utils.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
import os
def list_files_n_dirs(path: str):
"""Returns a list of files and directories from the path
Args:
path (str): directory path
Returns:
[list, list]: list of files and directories
"""
files = []
dirs = []
file_sizes = []
for (_, dir_names, file_names) in os.walk(path):
files.extend(file_names)
dirs.extend(dir_names)
for file in file_names:
file = os.path.join(path, file)
file_sizes.append(file_size(file))
return files, file_sizes, dirs
def convert_bytes(num):
"""Convert bytes to MB.... GB... etc
Args:
num (float): number of bytes
"""
for x in ["bytes", "KB", "MB", "GB", "TB"]:
if num < 1000.0:
return "%3.1f %s" % (num, x)
num /= 1000.0
def file_size(file_path):
"""Return the file size
Args:
file_path (str): file path
"""
if os.path.isfile(file_path):
file_info = os.stat(file_path)
return convert_bytes(file_info.st_size)