0

I am trying to make a simple piece of software that automatically makes a list based on the file contents of any newly inserted USB memory stick.

using ubuntu 14.04.

now using os.listdir() and glob.glob() work fine on their own...

however when using it with pyinotify, and getting the input_dir for the os.listdir() or glob.glob() from the event.pathname ... I just get a blank list every time...

tried concatenating strings to add single quotes, double quotes etc... to no avail.

here's a snippet of code

#!/usr/bin/python

#notifier setup
import pyinotify, os, glob

wm=pyinotify.WatchManager()

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_CREATE(self,event):
        global path
        path = event.pathname
        usb_insertion()

def usb_insertion():
    print glob.glob(path+"/*")
    print "listdir", os.listdir(path)


handler=EventHandler()
notifier=pyinotify.Notifier(wm, handler)
wdd=wm.add_watch('/media', pyinotify.IN_CREATE, rec=True)

notifier.loop()
hayderOICO
  • 11
  • 1
  • Try printing the `path` variable so you can see what it is. Also, don't use a global here, use a parameter! – kenm Oct 29 '14 at 15:11
  • I have solved this in case anyone is interested. Adding time.sleep(2) just before doing glob or os.listdir() seems to have done it. – hayderOICO Oct 29 '14 at 15:16
  • 1
    You are encouraged to provide answers for your own questions, if you feel the solution will be useful to others. Otherwise, you can just delete the question. – chepner Oct 29 '14 at 15:18
  • You should notice that Ubuntu 14.04 is adding the mount points for removable medis as `/media/$USER/$DEVICENAME` and not directly in `/media`. A watch on /media will not show when they are mounted. Also you should consider to use the notifications for device changes provided by the system (DBus, udev rules) instead of a file system watch. – Klaus D. Oct 29 '14 at 15:28

1 Answers1

1

I have solved this in case anyone is interested.

Adding time.sleep(2) just before doing glob or os.listdir() seems to have done it.

hayderOICO
  • 11
  • 1