网站地图 联系我们

学习python首选-实战python学习班,专注于python高级工程师培养,python课程:包括Python核心编程技术、全栈开发、网络爬虫、人工智能等。python全栈工程师+提高工作经验,获取免费试听课程!
当前位置: python培训 > python学习 > 常见问题 > 正文

用python watchdog监视目录可行性如何

来源:肚皮舞 浏览量: 发布日期:20-08-31 10:23:41 调整字体大小【 【所属栏目:常见问题】

  有人用过 python watchdog来监视目录么?

  因为工作需要,需要写个程序监视一个目录,当有文件创建时,将该文件上传到远程FTP服务器。

  使用watchdog可以实现监视的功能,但如果我在受监控的目录里放入的是一个较大的文件,就会报

  IOError: [Errno 13] Permission denied: u'F:\xiongji.mp4'的错误。

  因为它获取的是文件的创建、修改、删除等事件。

  如何获取【当一个文件创建完成】的事件呢?

  代码执行环境是:windows 7, python 2.7

  import sys

  import time

  import os

  import ftplib

  from ConfigParser import SafeConfigParser

  from watchdog.observers import Observer

  from watchdog.events import FileSystemEventHandler

  class 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 result

  class 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_path

  class 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一下,继续尝试打开,直到访问这个文件时不抛出异常了,那就说明这个文件创建完了。

标签:
肚皮舞新闻动态News Center

肚皮舞视频

肚皮舞培训课程

联系卡伊丝contact us
联系我们
python培训「零基础入门到精通」python人工智能编程培训班-0798wz.com

咨询热线:18301225663