Skip to content

Commit d03841a

Browse files
authored
Improvement of the file locator
I must commend @YashAgrawal0 for the idea! It was just missing the human-readable size. If something isn't clear @YashAgrawal0 let me know, I will be glad to help! I love #Hacktoberfest :-) What about you?
1 parent 9923778 commit d03841a

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

FileLocator/code.py

+48-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
11
import os
22
from os.path import join
33

4-
location = str(input("Directory Location : "))
5-
extension = str(input("Extension of files to search for : "))
6-
7-
for (dirname, dirs, files) in os.walk(location):
8-
for filename in files:
9-
if filename.endswith('.'+extension) :
10-
thefile = os.path.join(dirname,filename)
11-
print(os.path.getsize(thefile), thefile)
4+
5+
def get_human_size(file_size):
6+
"""
7+
Provides a human-readable size from the
8+
given file size (in bytes).
9+
10+
:param int file_size:
11+
The file size to convert.
12+
13+
:rtype: str
14+
"""
15+
16+
for human_size_description in [
17+
"bib",
18+
"KiB",
19+
"MiB",
20+
"GiB",
21+
"TiB",
22+
"PiB",
23+
"EiB",
24+
"ZiB",
25+
"YiB",
26+
]:
27+
if file_size < 1024:
28+
human_size = f"{file_size:3.1f}{human_size_description}"
29+
break
30+
31+
file_size /= 1024.0
32+
33+
return human_size
34+
35+
36+
if __name__ == "__main__":
37+
search_location = str(input("Directory Location : ")).strip()
38+
extension = str(input("Extension of files to search for : ")).strip()
39+
40+
if extension.startswith("."):
41+
extension = extension[1:]
42+
43+
for dirname, _, files in os.walk(search_location):
44+
for filename in files:
45+
if filename.endswith(f".{extension}"):
46+
the_file = os.path.join(dirname, filename)
47+
48+
print(
49+
get_human_size(os.path.getsize(the_file)),
50+
the_file,
51+
)

0 commit comments

Comments
 (0)