-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.py
41 lines (33 loc) · 1.18 KB
/
count.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
import os
import sys
from typing import List, Optional
class FileSystem:
def __init__(self) -> None:
self.path: str = os.path.dirname(os.path.abspath(__file__))
def folder_structure(self, path: Optional[str] = None) -> List[str]:
if path is None:
path = self.path
structure: List[str] = []
try:
for root, dirs, files in os.walk(path):
level: int = root.replace(path, "").count(os.sep)
indent: str = " " * 4 * level
structure.append(f"{indent}{os.path.basename(root)}/")
subindent: str = " " * 4 * (level + 1)
for f in files:
structure.append(f"{subindent}{f}")
except OSError as e:
print(f"Error: Unable to access directory '{path}': {e}")
return structure
def __str__(self) -> str:
return f"FileSystem({self.path})"
def main() -> None:
fs = FileSystem()
print(f"Folder structure of current directory {fs.path}:")
for item in fs.folder_structure():
print(item)
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"An error occurred: {e}")