python自动检测U盘拔插 置顶!
工作中用到,记录一下。
自动检测U盘拔插,输出U盘盘符。
import subprocess
import os
import re,time
from shutil import copyfile
def sh(command, print_msg=True):
p = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
try:
result = p.stdout.read().decode('GBK')
except:
result = p.stdout.read().decode('utf-8')
if print_msg:
print(result)
return result
def usbpath():
if os.name == 'nt':
disks = sh("wmic logicaldisk get deviceid, description",
print_msg=False).split('\n')
print(disks)
for disk in disks:
if 'Removable' in disk:
return re.search(r'\w:', disk).group()
elif os.name == 'posix':
return sh('ll -a /media')[-1].strip()
else:
return sh('ls /Volumes')[-1].strip()
print(usbpath())
注意:
win7可以用。但在同事win10下边运行,检测不出来。。。。。
各位可以再进行测试。
转载请注明出处,如有错误,请帮忙指正,谢谢!
标题:python自动检测U盘拔插
作者:走在人生的路上z
地址:http://17mark.com/articles/2020/10/27/1603793082925.html
此处应有打赏

