python watchdog_python,watchdog_python watchdog如何检测一个文件创建完成,python,watchdog - phpStudy...

python watchdog如何检测一个文件创建完成

有人用过 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'])

相关阅读:

关于异步回调执行的问题

对main线程使用join方法,为什么main线程处于一直阻塞状态?

iOS:autolayout,多个label如何设置居中显示?

phpstorm怎么样显示换行符?

jquery有什么方法指定先执行完某一行再接着执行指点的一行

linux下dns劫持有什么好的解决办法

redis的key或者计数器有没有递减的功能?

url get参数大讨论,查找出type=iphone&color=white&order=1

APP接口安全问题

请问大家有什么好的资料学习node引擎ejs

如何gzip压缩jQuery的POST请求数据?

关于信息安全方面的问题

七牛云 表单上传 回调 没执行, 不知道是怎么回事

linux中进行源码编译安装软件的时候经常出现一些警告,一般都是什么函数uninitialized,要紧吗?

iOS UIWebview 加载HTMLString的问题

新手完全不懂tornado.concurrent.Future

如何得到新浪微博的热门话题前十

为什么本地测试正常的 Jekyll 博客部署到 Gitcafe Pages 后无法打开?

python request HTTPS报错54, 'Connection reset by peer

easyform表单验证成功但是不提交

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值