如何使用 Python 在 Windows 中检测闪存驱动器插件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1968539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect flash drive plug-in in Windows using Python?
提问by Mahela Munasinghe
I want to make my Windows computer run a Python script when it detects that a flash drive which has a particular name (for example "My drive") has been plugged in.
我想让我的 Windows 计算机在检测到插入了具有特定名称(例如“我的驱动器”)的闪存驱动器时运行 Python 脚本。
How can I achieve this?
我怎样才能做到这一点?
Should I use some tool in Windows or is there a way to write another Python script to detect the presence of a flash drive as soon as it is plugged in? (I'd prefer it if the script was on the computer.)
我应该在 Windows 中使用一些工具还是有办法编写另一个 Python 脚本来在插入闪存驱动器后立即检测它的存在?(如果脚本在计算机上,我更喜欢它。)
(I'm a programming newbie.. )
(我是编程新手..)
回答by mellow-yellow
Building on the "CD" approach, what if your script enumerated the list of drives, waited a few seconds for Windows to assign the drive letter, then re-enumerated the list? A python set could tell you what changed, no? The following worked for me:
建立在“CD”方法的基础上,如果您的脚本枚举驱动器列表,等待几秒钟让 Windows 分配驱动器号,然后重新枚举列表怎么办?一个 python 集可以告诉你发生了什么变化,不是吗?以下对我有用:
# building on above and http://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-drive-letters-in-python
import string
from ctypes import windll
import time
import os
def get_drives():
drives = []
bitmask = windll.kernel32.GetLogicalDrives()
for letter in string.uppercase:
if bitmask & 1:
drives.append(letter)
bitmask >>= 1
return drives
if __name__ == '__main__':
before = set(get_drives())
pause = raw_input("Please insert the USB device, then press ENTER")
print ('Please wait...')
time.sleep(5)
after = set(get_drives())
drives = after - before
delta = len(drives)
if (delta):
for drive in drives:
if os.system("cd " + drive + ":") == 0:
newly_mounted = drive
print "There were %d drives added: %s. Newly mounted drive letter is %s" % (delta, drives, newly_mounted)
else:
print "Sorry, I couldn't find any newly mounted drives."
回答by UltraInstinct
Though you can use a similar method as 'inpectorG4dget' suggested but that will be a lot inefficient.
尽管您可以使用与建议的“inpectorG4dget”类似的方法,但这会非常低效。
You need to use Win API for this. This page might be of use to you: Link
为此,您需要使用 Win API。此页面可能对您有用:链接
And to use Win API's in python check this link out: Link
要在 python 中使用 Win API,请查看此链接:链接
回答by inspectorG4dget
Well, if you're on a Linux distribution, then this questionon SO would have the answer.
好吧,如果您使用的是 Linux 发行版,那么SO 上的这个问题就会有答案。
I can think of a round-about (not elegant) solution for your problem, but at the very least it would WORK.
我可以为您的问题想到一个迂回(不优雅)的解决方案,但至少它会起作用。
Every time you insert your flash drive into a USB port, the Windows OS assigns a drive letter to it. For the purposes of this discussion, let's call that letter 'F'.
每次将闪存驱动器插入 USB 端口时,Windows 操作系统都会为其分配一个驱动器号。出于本次讨论的目的,我们将该字母称为“F”。
This code looks to see if we can cd into f:\
. If it is possible to cd into f:\
, then we can conclude that 'F' has been allocated as a drive letter, and under the assumption that your flash drive always gets assigned to 'F', we can conclude that your flash drive has been plugged in.
此代码查看我们是否可以 cd 进入f:\
. 如果可以 cd into f:\
,那么我们可以得出结论“F”已被分配为驱动器号,并且假设您的闪存驱动器始终分配给“F”,我们可以得出结论,您的闪存驱动器已被插入在。
import os
def isPluggedIn(driveLetter):
if os.system("cd " +driveLetter +":") == 0: return True
else: return False