首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Windows OS: Python Watchdog检测“已删除”文件的目标文件路径

Windows OS是微软公司开发的操作系统,Python Watchdog是一个Python库,用于监控文件系统中的文件和目录的变化。它可以检测文件的创建、修改、删除等操作,并触发相应的事件。

在Windows操作系统中,当文件被删除时,它实际上并没有被完全删除,而是被移动到了回收站或者标记为已删除。Python Watchdog可以通过监控文件系统的变化来检测这些已删除文件的目标文件路径。

具体实现的步骤如下:

  1. 导入Watchdog库:在Python代码中,首先需要导入Watchdog库,可以使用以下代码实现:
代码语言:txt
复制
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
  1. 创建事件处理类:创建一个继承自FileSystemEventHandler的类,用于处理文件系统事件。可以使用以下代码创建一个事件处理类:
代码语言:txt
复制
class MyHandler(FileSystemEventHandler):
    def on_deleted(self, event):
        # 处理文件删除事件
        print("File deleted: " + event.src_path)
  1. 创建观察者对象:创建一个Observer对象,并将事件处理类与观察者对象关联。可以使用以下代码创建一个观察者对象:
代码语言:txt
复制
observer = Observer()
event_handler = MyHandler()
observer.schedule(event_handler, path='.', recursive=True)
  1. 启动观察者:启动观察者对象,开始监控文件系统的变化。可以使用以下代码启动观察者:
代码语言:txt
复制
observer.start()
  1. 监听事件:观察者对象会不断监听文件系统的变化,当有文件删除事件发生时,事件处理类中的on_deleted方法会被调用。可以在该方法中获取已删除文件的目标文件路径,并进行相应的处理。

推荐的腾讯云相关产品:腾讯云对象存储(COS) 腾讯云对象存储(COS)是一种高可用、高可靠、强安全的云端存储服务,适用于存储和处理任意类型的文件,包括文档、图片、音视频等。它提供了简单易用的API接口,可以方便地进行文件的上传、下载、删除等操作。腾讯云对象存储还具备数据冗余、数据加密、访问权限控制等功能,保障数据的安全性和可靠性。

腾讯云对象存储产品介绍链接地址:https://cloud.tencent.com/product/cos

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

Python 文件复制&按目录树结构拷贝&批量删除目录及其子目录下的文件

#!/usr/bin/env/ python # -*- coding:utf-8 -*- __author__ = 'shouke' import os import subprocess # 复制文件或目录到指定目录(非自身目录) def copy_dir_or_file(src, dest): if not os.path.exists(dest): print('目标路径:%s 不存在' % dest) return [False, '目标路径:%s 不存在' % dest] elif not os.path.isdir(dest): print('目标路径:%s 不为目录' % dest) return [False, '目标路径:%s 不为目录' % dest] elif src.replace('/', '\\').rstrip('\\') == dest.replace('/', '\\').rstrip('\\'): print('源路径和目标路径相同,无需复制') return [True,'源路径和目标路径相同,不需要复制'] if not os.path.exists(src): print('源路径:%s 不存在' % src) return [False, '源路径:%s 不存在' % src] # /E 复制目录和子目录,包括空的 /Y 无需确认,自动覆盖已有文件 args = 'xcopy /YE ' + os.path.normpath(src) + ' ' + os.path.normpath(dest) # 注意:xcopy不支持 d:/xxx,只支持 d:\xxxx,所以要转换 try: with subprocess.Popen(args, shell=True, universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: output = proc.communicate() print('复制文件操作输出:%s' % str(output)) if not output[1]: print('复制目标文件|目录(%s) 到目标目录(%s)成功' % (src, dest)) return [True,'复制成功'] else: print('复制目标文件|目录(%s) 到目标目录(%s)失败:%s' % (src, dest, output[1])) return [False,'复制目标文件|目录(%s) 到目标目录(%s)失败:%s' % (src, dest, output[1])] except Exception as e: print('复制目标文件|目录(%s) 到目标目录(%s)失败 %s' % (src, dest, e)) return [False, '复制目标文件|目录(%s) 到目标目录(%s)失败 %s' % (src, dest, e)] # 删除指定目录及其子目录下的所有子文件,不删除目录 def delete_file(dirpath): if not os.path.exists(dirpath): print('要删除的目标路径:%s 不存在' % dirpath) return [False, '要删除的目标路径:%s 不存在' % dirpath] elif not os.path.isdir(dirpath): print('要删除的目标路径:%s 不为目录' % dirpath) return [False, '要删除的目标路径:%s 不为目录' % dirpath] # 注意:同xcopy命令,del也不支持 d:/xxxx,Linux/Unix路径的写法,只支持d:\xxx windows路径的写法 args = 'del /F/S/Q ' + os.path.normpath(dirpath) # /F 强制删除只读文件。 /S 删除所有子目录中的指定的文件。 /Q 安静模式。删除前,不要求确认 try: with subprocess.Popen(args, shell=True, universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:

02

Python 基于Python实现的ssh兼sftp客户端(下)

#!/usr/bin/env/ python # -*- coding:utf-8 -*- __author__ = 'laifuyu' import os import subprocess class OtherTools: def __init__(self): self.filepath_list = [] # 批量创建目录 def mkdirs_once_many(self, path): path = os.path.normpath(path) # 去掉路径最右侧的 \\ 、/ path = path.replace('\\', '/') # 将所有的\\转为/,避免出现转义字符串 head, tail = os.path.split(path) new_dir_path = '' # 反转后的目录路径 root = '' #根目录 if not os.path.isdir(path) and os.path.isfile(path): # 如果path指向的是文件,则继续分解文件所在目录 head, tail = os.path.split(head) if tail == '': return while tail: new_dir_path = new_dir_path + tail + '/' head, tail = os.path.split(head) root = head else: new_dir_path = root + new_dir_path # 批量创建目录 new_dir_path = os.path.normpath(new_dir_path) head, tail = os.path.split(new_dir_path) temp = '' while tail: temp = temp + '/' + tail dir_path = root + temp if not os.path.isdir(dir_path): os.mkdir(dir_path) head, tail = os.path.split(head)

02
领券