【问题标题】:Detect external hard drive after letter changed in Python在Python中更改字母后检测外部硬盘驱动器
【发布时间】:2014-08-22 03:09:19
【问题描述】:

我正在编写一个 Python 程序来扫描和跟踪多个外部硬盘驱动器上的文件。它将文件路径作为字符串保存在本地文件中。问题是有时当我将外置硬盘插入不同的计算机时,字母会发生变化,并且之前存储的路径将无用。如果插入了相同的硬盘驱动器但字母已更改,我想跟踪驱动器并更改本地记录。目前,我能想到两种可能:

  1. 在驱动器的根目录中保留一个标识文件并扫描所有驱动器号以检测具有正确标识文件的驱动器。
  2. 扫描开头的所有字母以检测与本地记录在同一路径中的文件。如果找到,请识别驱动器。

我想知道是否有任何现有的 HDD(或分区)标识可用于访问驱动器(驱动器号除外)?

【问题讨论】:

标签: python windows hdd


【解决方案1】:

使用供应商 ID 和设备 ID 来识别驱动器。

#!/usr/bin/python
import sys
import usb.core
# find USB devices
dev = usb.core.find(find_all=True)
# loop through devices, printing vendor and product ids in decimal and hex
for cfg in dev:
  sys.stdout.write('Decimal VendorID=' + str(cfg.idVendor) + ' & ProductID=' + str(cfg.idProduct) + '\n')
  sys.stdout.write('Hexadecimal VendorID=' + hex(cfg.idVendor) + ' & ProductID=' + hex(cfg.idProduct) + '\n\n')

Use PyUSB to Find Vendor and Product IDs for USB Devices

类似问题:usb device identification

【讨论】:

    【解决方案2】:

    是的,您可以使用卷序列号来识别硬盘。卷序列号由 Windows 在您创建或格式化分区时生成。

    您应该可以通过 Python 使用以下代码执行此操作,将 c 替换为您想要的分区。:

    import subprocess
    
    subprocess.check_output(["vol", "C:"])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-03
      • 2017-02-21
      • 2016-12-17
      • 2013-08-08
      • 2011-12-31
      • 2010-12-12
      • 2020-08-14
      • 1970-01-01
      相关资源
      最近更新 更多