简体   繁体   English

如何使用python检测Windows中的备份驱动器?

[英]How to detect backup drive in windows using python?

I have 2 drives, OS drive and the backup drive in windows. 我在Windows中有2个驱动器,OS驱动器和备份驱动器。

I want to write a python script that detects the backup drive and returns the letter it is assigned and makes sure it is an external disk not thumbdrive or dvdrom . 我想编写一个Python脚本来检测备份驱动器并返回分配给它的字母,并确保它是一个外部磁盘,而不是thumbdrivedvdrom The letter assigned to the drive can vary. 分配给驱动器的字母可能会有所不同。

Is there a way to do that in python? 有没有办法在python中做到这一点? I have been searching through but to no avail. 我一直在搜索,但无济于事。

Expanding on my comment 扩展我的评论

Is there a way to do that in python? 有没有办法在python中做到这一点?

I think the short answer is: No. Not in Python, not in another language. 我认为简短的答案是:不。不是Python,也不是其他语言。

I want to write a python script that detects the backup drive 我想编写一个检测备份驱动器的python脚本

I don't think there's a way to do this. 我认为没有办法做到这一点。 There's nothing inherent to a drive that could be used to detect whether a connected drive is intended by the user to be a "backup" drive or for something else. 没有固有的驱动器可用于检测用户是否将连接的驱动器用作“备份”驱动器或用于其他用途。 In other words, whether a drive is a "backup" drive or not is determined by the user's behavior, not by the properties of the drive itself. 换句话说,驱动器是否为“备份”驱动器取决于用户的行为,而不是取决于驱动器本身的属性。

There're flags that can be set when a drive gets formatted (eg whether it's a bootable drive or not, etc), but that's about it. 格式化驱动器时可以设置一些标志(例如,是否为可引导驱动器等),仅此而已。

If we're talking about a method that's intended for your personal use only , then something that might work is the following: 如果我们在谈论的是一种仅供您个人使用的方法,则可能有效的方法如下:

  1. Create a naming convention for your drives (ie their labels when formatting), such as making sure your backup drives have the word "backup" somewhere in it; 创建驱动器的命名约定(即格式化时的标签),例如确保备份驱动器中的某个位置带有“ backup”一词;
  2. Make sure you never deviate from this naming convention; 确保您从未偏离此命名约定;
  3. Write a program that will iterate over your drives looking for the word "backup" in their names (a simple regular expression would work). 编写一个程序,该程序将遍历驱动器,在其名称中查找单词“ backup”(一个简单的正则表达式将起作用)。

Obviously, this would only work as long as the convention is followed. 显然,这只有在遵守约定的情况下才有效。 This is not a solution that you can arbitrarily apply in other situations where this assumption does not hold. 在此假设不成立的其他情况下,您不能随意应用此解决方案。

makes sure it is an external disk not thumbdrive or dvdrom. 确保它是外部磁盘,而不是thumbdrive或dvdrom。

This one might be tricky. 这可能很棘手。 If you connect an external HDD into a USB plug, the system would know the drive's capacity and the fact that it's connected through the USB interface, but I think that's about it. 如果将外部硬盘驱动器连接到USB插头,系统将知道驱动器的容量以及通过USB接口连接驱动器的事实,但是我想就是这样。

I have found a way to detect the drive if its a external HDD using python. 我发现了一种使用python检测驱动器是否为外部硬盘的方法。

import wmi
c = wmi.WMI()
for disk in c.Win32_LogicalDisk(["Caption", "VolumeName"], DriveType=3):

print disk

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM