-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
153 lines (118 loc) · 4.47 KB
/
Copy pathproject.py
File metadata and controls
153 lines (118 loc) · 4.47 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from pathlib import Path
import shutil
import datetime
from zipfile import ZipFile
import argparse
import sys
import humanize
from rich.console import Console
from rich.table import Table
console = Console()
def main():
parser = argparse.ArgumentParser(description="Organize, inspect, and analyze your filesystem!")
parser.add_argument("options", choices=["organize", "inspect", "analyze"], nargs='?')
parser.add_argument("path", nargs='?')
args = parser.parse_args()
if len(sys.argv) == 1:
menu()
else:
try:
path = convert_to_path(args.path)
match args.options:
case "organize":
folder_organizer(path)
case "inspect":
archive_inspector(path)
case "analyze":
folder_analyzer(path)
except FileNotFoundError:
sys.exit("File or folder does not exist!")
def menu():
try:
table = Table(title="OrgaFILEzer Menu")
table.add_column("Options", style="bold")
table.add_row("1. Folder Organizer")
table.add_row("2. Folder Analyzer")
table.add_row("3. Archive Inspector")
console.print(table)
user_choice = int(input("Options (1-3): "))
path = convert_to_path(input("Folder/file path: "))
match user_choice:
case 1:
folder_organizer(path)
case 2:
folder_analyzer(path)
case 3:
archive_inspector(path)
case _:
sys.exit("Invalid options!")
except ValueError:
sys.exit("Please insert a type according to the prompt!")
except FileNotFoundError:
sys.exit("File or folder does not exist!")
def folder_organizer(path: Path):
for file in path.iterdir():
if file.is_file():
target_dir = path / f"{file.suffix.upper().removeprefix(".")} files"
target_dir.mkdir(exist_ok=True)
shutil.move(file, target_dir)
print()
console.print("Success!", style="bold green")
def folder_analyzer(path: Path):
statistic = {
"total_files": 0,
"total_folders": 0,
"oldest_file": "",
"newest_file": "",
"total_size": 0,
"largest_file": "",
"smallest_file": ""
}
file_time = {}
file_size = {}
for file in path.rglob("*"):
if file.is_file():
statistic["total_files"] += 1
statistic["total_size"] += file.stat().st_size
file_time[file.relative_to(path)] = datetime.datetime.fromtimestamp(file.stat().st_mtime)
file_size[file.relative_to(path)] = file.stat().st_size
for folder in path.rglob("*"):
if folder.is_dir():
statistic["total_folders"] += 1
statistic["oldest_file"] = min(file_time, key=file_time.get)
statistic["newest_file"] = max(file_time, key=file_time.get)
statistic["largest_file"] = max(file_size, key=file_time.get)
statistic["smallest_file"] = min(file_size, key=file_time.get)
table = Table(title="Folder Analyzer")
table.add_column("List", style="bold")
table.add_column("Value")
table.add_row("Total files", humanize.intcomma(statistic["total_files"]))
table.add_row("Total folders:", humanize.intcomma(statistic["total_folders"]))
table.add_row("Oldest file:", str(statistic["oldest_file"]))
table.add_row("Newest file:", str(statistic["newest_file"]))
table.add_row("Total size:", humanize.naturalsize(statistic["total_size"]))
table.add_row("Largest file:", str(statistic["largest_file"]))
table.add_row("Smallest file:", str(statistic["smallest_file"]))
console.print(table)
def archive_inspector(path: Path):
try:
with ZipFile(path) as file:
print()
table = Table(title="Archive Inspector")
table.add_column("File Member Name", style="bold")
table.add_column("Size")
for f in file.infolist():
table.add_row(f.filename, humanize.naturalsize(f.file_size))
console.print(table)
except IsADirectoryError:
sys.exit("Please enter zip file")
def convert_to_path(path_string):
try:
path = Path(path_string).expanduser()
if not path.is_absolute():
path = Path.home() / path
return path
except TypeError:
sys.exit("Please enter path!")
if __name__ == "__main__":
main()