在帶有python的Mac OS上如何僅列出可寫卷?換句話說,在 /Volumes 檔案夾中,我只想列出(磁區和筆式驅動器)rw 我不想列出 CDROM 驅動器或安裝的 ISO 映像。
在 linux 中有檔案“/proc/mounts”,它顯示已安裝的驅動器以及磁區型別和安裝選項,在 mac OS 中是否有類似的東西?在 linux 中,我是這樣使用的:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from pathlib import Path
import getpass
def get_lst_writable_linux_disks():
this_user = getpass.getuser()
lst_available_linux_disks = [str(Path.home())]
with open('/proc/mounts','r') as f:
data = f.readlines()
for line in data:
item = line.split(' ')
mount_point = item[1]
fs_type = item[2]
options = item[3]
if mount_point.startswith('/mnt') or (mount_point.startswith(f'/media/{this_user}') and fs_type != 'vfat' and 'rw' in options):
lst_available_linux_disks.append(mount_point)
return lst_available_linux_disks
print(get_lst_writable_linux_disks())
我如何在 Mac OS 上做同樣的事情?
uj5u.com熱心網友回復:
對于比決議 的人類可讀輸出更防彈的方法diskutil info -all,您可以執行以下操作...(我不太喜歡 Apple 的 XML plist 格式;您會認為有更好的方法來表示 dict比平面鍵值鍵值-...結構...)
import subprocess
import xml.etree.ElementTree as ET
from typing import Tuple, List, Dict
def plist_dict_to_dict(node: ET.Element) -> Dict[str, ET.Element]:
assert node.tag == "dict"
dct = {}
current_key = None
for i, el in enumerate(node):
if i % 2 == 0:
assert el.tag == "key"
current_key = el.text
else:
assert current_key
dct[current_key] = el
return dct
def get_volume_names() -> List[str]:
command = ["/usr/sbin/diskutil", "list", "-plist"]
volumes_xml = ET.fromstring(subprocess.check_output(command, encoding="utf-8"))
volumes_info = plist_dict_to_dict(volumes_xml.find("dict"))
vfd_array = volumes_info["VolumesFromDisks"]
assert vfd_array.tag == "array"
return [v.text for v in vfd_array.findall("string")]
def get_volume_info(volume_name: str) -> Dict[str, ET.Element]:
command = ["/usr/sbin/diskutil", "info", "-plist", volume_name]
vol_info_xml = ET.fromstring(subprocess.check_output(command, encoding="utf-8"))
return plist_dict_to_dict(vol_info_xml.find("dict"))
def get_volume_flags(volume_name: str) -> Dict[str, bool]:
vol_info = get_volume_info(volume_name)
flags = {}
for key, value in vol_info.items():
if value.tag in ("true", "false"):
flags[key] = value.tag == "true"
return flags
if __name__ == "__main__":
for volume_name in get_volume_names():
print(volume_name, ":", get_volume_flags(volume_name))
在我的機器上,這會列印出來
Volume Macintosh HD : {'AESHardware': True, 'Bootable': True, 'CanBeMadeBootable': False, 'CanBeMadeBootableRequiresDestroy': False, 'Ejectable': False, 'EjectableMediaAutomaticUnderSoftwareControl': False, 'EjectableOnly': False, 'Encryption': True, 'FileVault': True, 'Fusion': False, 'GlobalPermissionsEnabled': True, 'Internal': True, 'Locked': False, 'PartitionMapPartition': False, 'RAIDMaster': False, 'RAIDSlice': False, 'Removable': False, 'RemovableMedia': False, 'RemovableMediaOrExternalDevice': False, 'SolidState': True, 'SupportsGlobalPermissionsDisable': True, 'SystemImage': False, 'WholeDisk': False, 'Writable': False, 'WritableMedia': True, 'WritableVolume': False}
Volume Macintosh HD - Data : {'AESHardware': True, 'Bootable': True, 'CanBeMadeBootable': False, 'CanBeMadeBootableRequiresDestroy': False, 'Ejectable': False, 'EjectableMediaAutomaticUnderSoftwareControl': False, 'EjectableOnly': False, 'Encryption': True, 'FileVault': True, 'Fusion': False, 'GlobalPermissionsEnabled': True, 'Internal': True, 'Locked': False, 'PartitionMapPartition': False, 'RAIDMaster': False, 'RAIDSlice': False, 'Removable': False, 'RemovableMedia': False, 'RemovableMediaOrExternalDevice': False, 'SolidState': True, 'SupportsGlobalPermissionsDisable': True, 'SystemImage': False, 'WholeDisk': False, 'Writable': True, 'WritableMedia': True, 'WritableVolume': True}
Volume Recovery : {'AESHardware': True, 'Bootable': False, 'CanBeMadeBootable': False, 'CanBeMadeBootableRequiresDestroy': False, 'Ejectable': False, 'EjectableMediaAutomaticUnderSoftwareControl': False, 'EjectableOnly': False, 'Encryption': False, 'FileVault': False, 'Fusion': False, 'GlobalPermissionsEnabled': True, 'Internal': True, 'Locked': False, 'PartitionMapPartition': False, 'RAIDMaster': False, 'RAIDSlice': False, 'Removable': False, 'RemovableMedia': False, 'RemovableMediaOrExternalDevice': False, 'SolidState': True, 'SupportsGlobalPermissionsDisable': True, 'SystemImage': False, 'WholeDisk': False, 'Writable': True, 'WritableMedia': True, 'WritableVolume': True}
Volume VM : {'AESHardware': True, 'Bootable': False, 'CanBeMadeBootable': False, 'CanBeMadeBootableRequiresDestroy': False, 'Ejectable': False, 'EjectableMediaAutomaticUnderSoftwareControl': False, 'EjectableOnly': False, 'Encryption': True, 'FileVault': False, 'Fusion': False, 'GlobalPermissionsEnabled': True, 'Internal': True, 'Locked': False, 'PartitionMapPartition': False, 'RAIDMaster': False, 'RAIDSlice': False, 'Removable': False, 'RemovableMedia': False, 'RemovableMediaOrExternalDevice': False, 'SolidState': True, 'SupportsGlobalPermissionsDisable': True, 'SystemImage': False, 'WholeDisk': False, 'Writable': True, 'WritableMedia': True, 'WritableVolume': True}
所以除了“可寫”之外,您可能還想看看“內部”......
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/342726.html
