![](/img/trans.png)
[英]How to run a python script with imports when usb plugged into Linux machine?
[英]Python/Linux-How to create a script that monitor USB ports and print the mount point path when a new USB Storage Device is plugged?
我的目標是在無限循環(僅通過鍵盤中斷停止)上運行腳本,當我插入設備時,代碼在屏幕上顯示“新驅動器安裝在/ mount / point / here”。 我嘗試使用pyudev,但無法正常工作。 誰能告訴我該怎么做? 我還是與Linux事件交互的新手。
對於設備監視,請在循環內使用以下代碼:
#!/usr/bin/python3
import pyudev
context = pyudev.Context()
path = pyudev.Devices.from_sys_path
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by('block')
# enumerate at device connection
for device in iter(monitor.poll, None):
if 'ID_FS_TYPE' in device:
if device.action == 'add':
if device.device_node[5:7] == 'sd':
print('New drive mounted at /', device.device_node)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.