【问题标题】:How to detect flash drive plug-in in Windows using Python?如何使用 Python 检测 Windows 中的闪存驱动器插件?
【发布时间】:2009-12-28 08:26:50
【问题描述】:

我想让我的 Windows 计算机在检测到已插入具有特定名称(例如“我的驱动器”)的闪存驱动器时运行 Python 脚本。

我怎样才能做到这一点?

我应该在 Windows 中使用一些工具,还是有办法编写另一个 Python 脚本,以便在插入闪存驱动器后立即检测它是否存在? (如果脚本在计算机上,我会更喜欢它。)

(我是一个编程新手..)

【问题讨论】:

    标签: python windows usb-drive


    【解决方案1】:

    在“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."
    

    【讨论】:

      【解决方案2】:

      虽然您可以使用与建议的“inpectorG4dget”类似的方法,但效率会低很多。

      您需要为此使用 Win API。此页面可能对您有用:Link

      要在 python 中使用 Win API,请查看此链接:Link

      【讨论】:

      • 这可行,但需要安装至少一个额外的模块
      【解决方案3】:

      好吧,如果您使用的是 Linux 发行版,那么 SO 上的 this question 会给出答案。

      我可以为您的问题想出一个迂回(不优雅)的解决方案,但至少它会起作用。

      每次将闪存驱动器插入 USB 端口时,Windows 操作系统都会为其分配一个驱动器号。出于讨论的目的,我们称该字母为“F”。

      这段代码看看我们是否可以 cd 进入f:\。如果可以 cd 进入f:\,那么我们可以断定“F”已被分配为驱动器号,并且假设您的闪存驱动器总是被分配给“F”,我们可以断定您的闪存驱动器已插入。

      import os
      def isPluggedIn(driveLetter):
          if os.system("cd " +driveLetter +":") == 0: return True
          else: return False
      

      【讨论】:

      • 但是驱动器并不总是分配给同一个字母。我该如何解释呢?
      • 就是这样。我想不出办法马上做到这一点。但至少,这是部分解决方案。我只是发布它,因为当时没有其他解决方案。所以我认为部分解决方案总比没有好
      【解决方案4】:
      import subprocess
      
      out = subprocess.check_output('wmic logicaldisk get  DriveType, caption', shell=True)
      
      for drive in str(out).strip().split('\\r\\r\\n'):
          if '2' in drive:
              drive_litter = drive.split(':')[0]
              drive_type = drive.split(':')[1].strip()
              #print(drive_litter, drive_type)
              if drive_type == '2':
                  print('Removable disk detected')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-15
        • 2011-01-26
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        相关资源
        最近更新 更多