用python watchdog监视目录可行性如何,pythonwatchdog,有人用过 python
用python watchdog监视目录可行性如何,pythonwatchdog,有人用过 python
有人用过 python watchdog来监视目录么?
因为工作需要,需要写个程序监视一个目录,当有文件创建时,将该文件上传到远程FTP服务器。
使用watchdog可以实现监视的功能,但如果我在受监控的目录里放入的是一个较大的文件,就会报
IOError: [Errno 13] Permission denied: u'F:\xiongji.mp4'的错误。
因为它获取的是文件的创建、修改、删除等事件。
如何获取【当一个文件创建完成】的事件呢?
代码执行环境是:windows 7, python 2.7
import sysimport timeimport osimport ftplibfrom ConfigParser import SafeConfigParserfrom watchdog.observers import Observerfrom watchdog.events import FileSystemEventHandlerclass MyConfig(): def __init__(self): self.loaded = False self.scp = None @staticmethod def instance(): if not hasattr(MyConfig, "_instance"): MyConfig._instance = MyConfig() return MyConfig._instance def load_config(self, filename): self.scp = SafeConfigParser() self.scp.read(filename) self.loaded = True return self @property def loaded(self): return self.loaded def get_segment(self, segment_name): result = {} keys = self.scp.options(segment_name) for k in keys: result[k] = self.scp.get(segment_name,k) return resultclass MyFTPClient(): def __init__(self): self.loaded = False self.ftp = None @staticmethod def instance(): if not hasattr(MyFTPClient, "_instance"): MyFTPClient._instance = MyFTPClient() return MyFTPClient._instance @property def loaded(self): return self.loaded def load_config(self, configfile): if not MyConfig().instance().loaded: MyConfig().instance().load_config(configfile) host = MyConfig().instance().get_segment('FTP Config master')['ftp_host'] self.ftp = ftplib.FTP(host) user = MyConfig().instance().get_segment('FTP Config master')['ftp_user'] passwd = MyConfig().instance().get_segment('FTP Config master')['ftp_passwd'] ftp_pwd = MyConfig().instance().get_segment('FTP Config master')['ftp_pwd'] self.ftp.login(user, passwd) self.ftp.cwd(ftp_pwd) def upload_file(self, file_abs_path): command = 'STOR ' + os.path.basename(file_abs_path) os.chdir(os.path.dirname(file_abs_path)) print "FTP command: [%s]" % command try: ret = self.ftp.storbinary(command, open(file_abs_path, "rb")) except ftplib.error_perm, e: print e.message #except: # print "unknown error." # pass print "upload [%s] O.K." % file_abs_pathclass MyFileMonitor(FileSystemEventHandler): def on_created(self, event): super(MyFileMonitor, self).on_created(event) if not event.is_directory: print "created name:[%s]" % event.src_path def on_modified(self, event): super(MyFileMonitor, self).on_created(event) if not event.is_directory: print "modified name:[%s]" % event.src_path abs_path = event.src_path MyFTPClient().instance().upload_file(abs_path)def monitor(path): event_handler = MyFileMonitor() observer = Observer() observer.schedule(event_handler, path, recursive=True) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()if __name__ == '__main__': MyConfig.instance().load_config('config.ini') MyFTPClient.instance().load_config('config.ini') monitor(MyConfig.instance().get_segment('User PC config')['picture_dir'])
猜测:当创建时,较大的文件没有完全拷贝完,这时候你访问就会抛出异常,那你sleep一下,继续尝试打开,直到访问这个文件时不抛出异常了,那就说明这个文件创建完了。
编橙之家文章,
相关内容
- Python3.0版本做web可以吗?,python3.0版本做web,在群里聊天
- Flask教程效果做不出来,谁来帮帮我NameError,flasknameerr
- 请问Django配合PyCharm开发Console,默认输出Log格式修改怎么
- flask-socketio web开发启动服务方式,,使用 flask-soc
- Python web.py输出到模板时除字符串引号应该如何操作,
- Python实现基于位置用户显示需求的方法是什么,python需
- pyquery大小写效果不理想,有更好的吗,pyquery大小写,感觉
- 怎么获取Python3模块路径列表的疑惑,获取python3模块路
- Python上传文件到七牛后文件名如何保存,python牛后,im
- Django语音文件在线播放功能调试方式是什么?,django在线
评论关闭