-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadvanced_disk_manager.py
More file actions
88 lines (76 loc) · 3.13 KB
/
advanced_disk_manager.py
File metadata and controls
88 lines (76 loc) · 3.13 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
import subprocess
import argparse
def list_disks():
"""Lists all disks (both partitioned and unpartitioned)."""
disks = []
# Check for physical disks (disk drives) using WMIC
result = subprocess.check_output("wmic diskdrive get caption", shell=True).decode()
# Extract disk names from the output
for line in result.splitlines():
if line.strip() and line.strip().lower() != "caption":
disks.append(line.strip())
return disks
def list_partitions(disk):
"""Lists partitions for a given disk."""
partitions = []
try:
result = subprocess.check_output(f"wmic logicaldisk where \"DeviceID='{disk}'\" get caption", shell=True).decode()
for line in result.splitlines():
if line.strip() and line.strip().lower() != "caption":
partitions.append(line.strip())
except subprocess.CalledProcessError:
pass
return partitions
def disk_health_check(disk):
"""Checks the disk for bad sectors."""
try:
result = subprocess.run(
["chkdsk", disk],
text=True,
capture_output=True,
check=True,
shell=True
)
print(f"Health check for {disk}:\n\n{result.stdout}")
except Exception as e:
print(f"Failed to perform health check on {disk}: {str(e)}")
def format_disk(disk, file_system="NTFS", label="NewVolume"):
"""Formats the specified disk with user-selected options."""
try:
disk_commands = f"""
select disk {disk}
clean
create partition primary
format fs={file_system.lower()} label={label} quick
assign
"""
subprocess.run(["diskpart"], input=disk_commands, text=True, check=True, shell=True)
print(f"Disk {disk} has been formatted with {file_system} and label '{label}'.")
except Exception as e:
print(f"Failed to format disk {disk}: {str(e)}")
def handle_disk_operations(action, disk, file_system="NTFS", label="NewVolume"):
"""Handles disk operations based on user action."""
disks = list_disks()
if disk not in disks:
print(f"Disk {disk} is not available.")
return
partitions = list_partitions(disk)
if partitions:
print(f"Disk {disk} already has partitions: {', '.join(partitions)}")
return
if action == "healthcheck":
disk_health_check(disk)
elif action == "format":
format_disk(disk, file_system, label)
else:
print("Invalid action specified.")
# Main CLI logic
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Disk Management Tool")
parser.add_argument("--action", choices=["format", "healthcheck"], required=True, help="Action to perform on the disk")
parser.add_argument("--disk", required=True, help="Disk to operate on (e.g., 'C:', 'D:')")
parser.add_argument("--fs", default="NTFS", help="File system to format with (e.g., 'NTFS', 'FAT32')")
parser.add_argument("--label", default="NewVolume", help="Volume label for the formatted disk")
args = parser.parse_args()
handle_disk_operations(args.action, args.disk, args.fs, args.label)
#i was here hehe