python - 在 Python 中更改字母后检测外部硬盘驱动器

标签 python windows hard-drive

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

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

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

最佳答案

使用供应商 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

关于python - 在 Python 中更改字母后检测外部硬盘驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24512265/

相关文章:

python - 选择数组的每 n 个索引

windows - 我如何使用 Apache 2 忽略 Windows 上的 Perl shebang?

arrays - 搜寻时间与顺序读取

windows - 如何检测硬盘驱动器是否正在旋转(在 Windows 下)?

c - 需要 libc API 来访问磁盘几何和分区相关信息

python - 素数算法在某个点后停止工作

python - 使用正则表达式检查密码的强度

python - 根据具有特定值的行创建新的 Dataframe 并从原始 Dataframe 中删除行

Java 日志文件包含重复的记录

c - 使用记事本构建 C 应用程序的自动化工具