I'm currently having a problem on how can I run a python function once the user mounts a flash drive. I have a GUI that's in idle mode and the user can start using when he/she plugs a flash drive. Your help will be very much appreciated :) 
Asked
Active
Viewed 576 times
0
seluj rufo
- 3
- 2
-
This previous answer might help: https://stackoverflow.com/questions/7115731/linux-usb-connect-disconnect-event#7951860 – NameGoesHere Aug 26 '18 at 14:28
-
1It isn't really specific for Raspberry Pi. – Ingo Aug 26 '18 at 17:29
-
You should give it a try and post what you did so that we can make suggestions based on that. – Brick Aug 27 '18 at 15:22
-
One way to achieve that is called "udev rules". – Dmitry Grigoryev Aug 28 '18 at 09:39
-
Hello again! The flash drive can be recognized now using pyudev. I'm moving on to my next problem which is how to change the content displayed when a flash drive is detected. Thank you guys for your help! – seluj rufo Aug 29 '18 at 07:06
1 Answers
0
You can run python script with boot so it'll be running all time
Here is a example code that ejects when any usb mounted. You can edit the code to make what you want :
'''
Usage: python3 usb_eject.py
OS: Window7 and later
Eject the usb storage when the usb device plugin your PC!
'''
from time import sleep
import http.client
import subprocess
def monitorUSBStorage():
label = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',
'T','U','V','W','X','Y','Z']
monitorDisk = []
for i in label:
try:
file = open(i+':/')
except Exception as e:
'''
error = 2 =>not found
error = 13 =>permission denied (exist!)
'''
if(e.errno == 13):
print("Disk : "+i+" Exist!")
else:
monitorDisk.append(i)
print("Start monitoring.....")
while(True):
print("Check...")
isININ = False;
disk = '';
for i in monitorDisk:
try:
file = open(i+':/')
except Exception as e:
if(e.errno == 13):
print("Disk : "+i+" Exist!")
isININ = True
disk = i
break
if(isININ):
tmpFile = open('tmp.ps1','w')
tmpFile.write('$driveEject = New-Object -comObject Shell.Application\n')
tmpFile.write('$driveEject.Namespace(17).ParseName("'+disk+':").InvokeVerb("Eject")')
tmpFile.close()
process = subprocess.Popen(['powershell.exe', '-ExecutionPolicy','Unrestricted','./tmp.ps1'])
process.communicate()
#sleep for 2 seconds
sleep(0.5)
if __name__ == '__main__':
monitorUSBStorage()
To use it in raspi you can change usb listing command by the light of that topic
- Code is from Github/iamgyz Link
CanSevgi
- 40
- 1
- 8