Does Python detect a USB drive and then assign a drive letter?

Here is the problem. We have 100 external USB drives for 500 GB. Each drive will go to a new location throughout the year. What is the best way to automatically detect that a USB drive is connected to a Windows system and then assign the drive letter Z: \? These USB drives will be connected to many different computers, so a script like this

import subprocess

diskpart_data = "z-drive.txt"
open (diskpart_data, "w").write ("""
select volume F: 
assign letter=Z
""")
subprocess.call ('diskpart /s %s' % diskpart_data)

difficult to use due to the dynamic nature of the mobile USB drive in different Windows systems all the time? Can you automatically detect through WMI or do some kind of mounting volumes using NTFS?

+3
source share
2 answers

terabytest, script autorun.inf . script:

(echo select volume %~d0 && echo assign letter=Z) | diskpart

%~d0 .

, , Windows API. RegisterDeviceNotification(), WM_DEVICECHANGE . , , , python ( : , , , ).

+2

script exe USB-, script. script . Z:\letter Win32Com ( , ) ctypes, DLL Windows.

+1

Source: https://habr.com/ru/post/1723469/


All Articles