通过 python 从卡 reader 读取和复制文件

reading and copying files from a card reader via python

我没有经常使用 python,但我想创建一个在后台运行并寻找插入的新驱动器的连续脚本。我希望脚本能够从驱动器中取出所有文件并将它们存储在我指定的另一个地方。这是有人可以帮助我的东西还是我应该去别处看看?谢谢!

这可能会让您入门:

import subprocess
import time

def get_drives():
    """Return a list of disk drives"""
    output = subprocess.check_output("fsutil fsinfo drives")
    return output[output.find(b":")+1:].decode("utf-8").split()

sleep_interval = 60 # seconds

while True:
    # get a list of the current drives
    drives = get_drives()
    try:
        time.sleep(sleep_interval)
        # check for new drives
        new_drives = get_drives()
        for drive in new_drives:
            if drive not in drives:
                print(drive, "detected")
                # do more stuff here
        # update stored drives so we can detect removal & reinsertion
        drives = new_drives
    except KeyboardInterrupt:
        print("Exiting")
        break

对于您的测试,您可能希望将睡眠间隔设置为一秒左右。 按 Ctrl+C 退出。

示例运行:

J:\>python drive_check.py
D:\ detected
Exiting