0

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 :) enter image description here

closed as off-topic by Ingo, Aurora0001, Steve Robillard, Dmitry Grigoryev, Gene Aug 29 '18 at 11:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – Ingo, Steve Robillard, Gene
If this question can be reworded to fit the rules in the help center, please edit the question.

  • This previous answer might help: stackoverflow.com/questions/7115731/… – NameGoesHere Aug 26 '18 at 14:28
  • 1
    It 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 9: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 7:06
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.