Python:如何仅搜索USB闪存驱动器? [英] Python: How to search only USB flash drives?

查看:120
本文介绍了Python:如何仅搜索USB闪存驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,该脚本旨在从USB闪存驱动器运行,如果从PC硬盘驱动器运行则无法运行,因此可以安全地假定所有副本都存在于连接的USB上.

I have a Python script which is designed to be run from a USB flash drive, and would not work if run from a PC hard drive, so it is safe to assume all copies exist on connected USBs.

我有另一个设计用于从计算机硬盘驱动器运行的脚本,该脚本会查找这些USB脚本并以某种方式配置所有这些脚本.显然,为了节省时间,当我知道它们仅在USB上时,我不想搜索整个硬盘.有没有办法只通过检查驱动器号等来搜索连接的USB上的文件,跳过搜索本地驱动器?

I have another script designed to be run from the computer hard drive which seeks these USB scripts and configures them all in a certain way. Obviously to save time I do not want to search the entire hard drive when I know they are only on USBs. Is there a way to only search files on a connected USB, skipping searching local drives, through checking drive letters or the like?

推荐答案

下面是一些示例代码,可使用ctypes ...

Here's some example code to determine the drive type for every active logical drive on Windows, using ctypes...

import ctypes

# Drive types
DRIVE_UNKNOWN     = 0  # The drive type cannot be determined.
DRIVE_NO_ROOT_DIR = 1  # The root path is invalid; for example, there is no volume mounted at the specified path.
DRIVE_REMOVABLE   = 2  # The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.
DRIVE_FIXED       = 3  # The drive has fixed media; for example, a hard disk drive or flash drive.
DRIVE_REMOTE      = 4  # The drive is a remote (network) drive.
DRIVE_CDROM       = 5  # The drive is a CD-ROM drive.
DRIVE_RAMDISK     = 6  # The drive is a RAM disk.

# Map drive types to strings
DRIVE_TYPE_MAP = { DRIVE_UNKNOWN     : 'DRIVE_UNKNOWN',
                   DRIVE_NO_ROOT_DIR : 'DRIVE_NO_ROOT_DIR',
                   DRIVE_REMOVABLE   : 'DRIVE_REMOVABLE',
                   DRIVE_FIXED       : 'DRIVE_FIXED',
                   DRIVE_REMOTE      : 'DRIVE_REMOTE',
                   DRIVE_CDROM       : 'DRIVE_CDROM',
                   DRIVE_RAMDISK     : 'DRIVE_RAMDISK'}


# Return list of tuples mapping drive letters to drive types
def get_drive_info():
    result = []
    bitmask = ctypes.windll.kernel32.GetLogicalDrives()
    for i in range(26):
        bit = 2 ** i
        if bit & bitmask:
            drive_letter = '%s:' % chr(65 + i)
            drive_type = ctypes.windll.kernel32.GetDriveTypeA('%s\\' % drive_letter)
            result.append((drive_letter, drive_type))
    return result


# Test
if __name__ == '__main__':
    drive_info = get_drive_info()
    for drive_letter, drive_type in drive_info:
        print '%s = %s' % (drive_letter, DRIVE_TYPE_MAP[drive_type])
    removable_drives = [drive_letter for drive_letter, drive_type in drive_info if drive_type == DRIVE_REMOVABLE]
    print 'removable_drives = %r' % removable_drives

...打印...

C: = DRIVE_FIXED
D: = DRIVE_FIXED
E: = DRIVE_CDROM
removable_drives = []

...在插入USB记忆棒和...之前...

...before inserting a USB stick and...

C: = DRIVE_FIXED
D: = DRIVE_FIXED
E: = DRIVE_CDROM
F: = DRIVE_REMOVABLE
removable_drives = ['F:']

...之后.

一旦有了可移动驱动器的列表,就可以简单地使用 os.walk() 在每个驱动器上.

Once you've got the list of removable drives, you can simply use os.walk() on each drive.

这篇关于Python:如何仅搜索USB闪存驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆