1
1
import shutil
2
2
import psutil
3
3
import os
4
+ import subprocess
5
+ import re
4
6
5
7
class hard_drive (object ):
6
8
total = 0
@@ -18,7 +20,7 @@ def __init__(self, total, free, drive_name):
18
20
19
21
class hard_drive_controller (object ):
20
22
available_drive_letters = ["A:/" , "B:/" , "C:/" , "D:/" , "E:/" , "F:/" , "G:/" , "H:/" , "I:/" , "J:/" , "K:/" , "L:/" , "M:/" , "N:/" , "O:/" , "P:/" , "Q:/" , "R:/" , "S:/" , "T:/" , "U:/" , "V:/" , "W:/" , "X:/" , "Y:/" , "Z:/" ]
21
- supported_hard_drive_formats = ['vfat' ,'ext4' , 'ext3' , 'ext2' , 'fat32' , 'ntfs' , 'fuseblk' ]
23
+ supported_hard_drive_formats = ['vfat' ,'ext4' , 'ext3' , 'ext2' , 'fat32' , 'ntfs' , 'fuseblk' , 'tmpfs' ]
22
24
23
25
def get_hard_drives_linux (self ):
24
26
hard_drives = []
@@ -34,13 +36,28 @@ def get_hard_drives_linux(self):
34
36
35
37
def get_hard_drives_windows (self ):
36
38
hard_drives = []
37
- for available_drive in self .available_drive_letters :
39
+ mountvol_hds = []
40
+ result = subprocess .run (['mountvol' ], capture_output = True , text = True )
41
+ parsed_hard_drives = re .findall ('[A-Z]:\\ \.*' , result .stdout )
42
+
43
+ for hd in parsed_hard_drives :
44
+ try :
45
+ formatted_hd = hd .replace ("\\ " ,"/" )
46
+ mountvol_hds .append (formatted_hd )
47
+ hd_object = self .get_hard_disk_space (formatted_hd )
48
+ hard_drives .append (hd_object )
49
+ except Exception :
50
+ continue
51
+
52
+ #network drives don't show up in mountvol. manually iterate to see if they have any network storage
53
+ not_checked_drives = [i for i in self .available_drive_letters if i not in mountvol_hds ]
54
+ for hd in not_checked_drives :
38
55
try :
39
- hd = self .get_hard_disk_space (available_drive )
40
- hard_drives .append (hd )
41
-
56
+ hd_object = self .get_hard_disk_space (hd )
57
+ hard_drives .append (hd_object )
42
58
except Exception :
43
59
continue
60
+
44
61
return hard_drives
45
62
46
63
def get_hard_drives (self ):
@@ -65,3 +82,4 @@ def get_hard_disk_space(self, drive):
65
82
66
83
if __name__ == '__main__' :
67
84
controller = hard_drive_controller ()
85
+ hds = controller .get_hard_drives_windows ()
0 commit comments