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, pandalion98 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, pandalion98
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

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',
monitorDisk = []
for i in label:
        file = open(i+':/')
    except Exception as e:
        error = 2  =>not found
        error = 13 =>permission denied (exist!)
        if(e.errno == 13):
            print("Disk : "+i+" Exist!")

print("Start monitoring.....")
    isININ = False;
    disk = '';
    for i in monitorDisk:
            file = open(i+':/')
        except Exception as e:
            if(e.errno == 13):
                print("Disk : "+i+" Exist!")
                isININ = True
                disk = i
        tmpFile = open('tmp.ps1','w')
        tmpFile.write('$driveEject = New-Object -comObject Shell.Application\n')
        process = subprocess.Popen(['powershell.exe', '-ExecutionPolicy','Unrestricted','./tmp.ps1'])
    #sleep for 2 seconds

if __name__ == '__main__':

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.