python pyinotify不能与os.listdir()或glob.glob()一起使用

时间:2014-10-29 15:05:58

标签: python python-2.7 ubuntu-14.04 pyinotify

我正在尝试制作一个简单的软件,根据任何新插入的USB记忆棒的文件内容自动生成一个列表。

使用ubuntu 14.04。

现在使用os.listdir()和glob.glob()可以自行工作......

但是当它与pyinotify一起使用时,从event.pathname获取os.listdir()或glob.glob()的input_dir ...我每次都会得到一个空白列表......

尝试连接字符串以添加单引号,双引号等......但无济于事。

这是一段代码

#!/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()

1 个答案:

答案 0 :(得分:1)

如果有人有兴趣,我已经解决了这个问题。

在执行glob或os.listdir()之前添加time.sleep(2)似乎已经完成了。