简体   繁体   English

python-从逻辑路径获取物理磁盘信息

[英]python-get physical disk info from logical path

I have the logical paths. 我有逻辑路径。 For instance:- path1='C:/abc', path2='E:/xyz' 例如:-path1 ='C:/ abc',path2 ='E:/ xyz'

I want to check if both of them reside on same physical disk. 我想检查它们是否都位于同一物理磁盘上。

Or in general I want to map a logical path to associated physical drives. 或者通常,我想将逻辑路径映射到关联的物理驱动器。

I would also want to do the same for linux. 我也想对linux做同样的事情。 For now I use "os.stat(path).st_dev" to get the device id. 现在,我使用“ os.stat(path).st_dev”来获取设备ID。 But the id seems to be different for logical disks on same same physical drive. 但是,同一物理驱动器上的逻辑磁盘的id似乎不同。 So it doesn't actully work for me. 因此,它对我不起作用。 Is there a better or direct solution to it. 是否有更好或更直接的解决方案。

This solved my problem 这解决了我的问题

   key = ''
    for physical_disk in w.Win32_DiskDrive():
    logical_disks = []
    for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):
        for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):
            key = logical_disk.Description
            if key not in all_logical_disks:
                all_logical_disks[key] = []
            logical_disks.append(logical_disk.Caption)
    if logical_disks:
        all_logical_disks[key].append(logical_disks)

This helped me to get a dict of type: {u'Local Fixed Disk': [[u'C:', u'D:'],[u'E:', u'F:']]} which grouped logical patitions in different physical disks. 这帮助我得到了以下类型的字典:{u'Local Fixed Disk':[[u'C:',u'D:'],[u'E:',u'F:']]}}不同物理磁盘中的逻辑分区。 Now I can compare if the logical paths lie in the same list. 现在,我可以比较逻辑路径是否在同一列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python-使用networkx获取网络节点的程度 - python-get the degree of network nodes with networkx python-获取子列表而不更改位置 - python-get a sublist without changing position 如何在python中获取/设置逻辑目录路径 - How to get/set logical directory path in python 使用Concurrent.Futures模块作为ThreadPool调用时,Python-get函数的返回值 - Python-get function's return value when called using Concurrent.Futures module as ThreadPool Python中逻辑线和物理线之间的区别 - Difference between logical line and physical line in python Python:试图在 3990X 上获取逻辑和物理内核的数量,得到错误的答案? - Python: Trying to get the number of logical and physical cores on a 3990X, getting the wrong answer? 使用Python在Linux上学习目录的物理磁盘 - Learning the directory's physical disk on Linux with Python Python3:是否有任何包/函数/脚本来获取对磁盘上包的元数据(egg-info 或 dist-info)的引用? - Python3: Is there any package/function/script to get a reference to a package's metadata (egg-info or dist-info) ON DISK? Python 和 GetVolumeInformationW 和 GetDriveTypeW:列出所有磁盘并获取磁盘信息和文件系统标志 - Python and GetVolumeInformationW and GetDriveTypeW: list all disks and get disk info and filesystem flags 无法在python中获取有关os.path属性的信息 - Can't get info on os.path attribute in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM