gpt4 book ai didi

python - ReadDirectoryChangesW 阻止删除监视的目录

转载 作者:太空狗 更新时间:2023-10-30 02:49:45 25 4
gpt4 key购买 nike

我正在尝试使用 ReadDirectoryChangesW API 使用 python 在 Windows 上查看目录以创建/删除/重命名更改。这是我的代码,它运行良好:

results = win32file.ReadDirectoryChangesW(self.hDir, 8192, True, self.type, None,
None)
for action, file in results:
full_filename = os.path.join (self.source_path, file)
if action == 1: # Created
self.fileCreated(full_filename)
elif action == 2: # Deleted
self.fileDeleted(full_filename)
elif action == 3: # Updated
self.fileUpdated(full_filename)
elif action == 4: # Renamed from something
renamed_file = full_filename
elif action == 5: # Renamed to something
self.fileRenamed(renamed_file, full_filename)

但是,当我尝试从 python 或 Windows 资源管理器中删除监视的文件夹时,我得到:

WindowsError: [Error 32] 该进程无法访问该文件,因为它正被另一个进程使用:'c:\users\user\appdata\local\temp\new_dir'

我相信这是有道理的,但我应该如何解决这个问题?因为我的应用程序应该允许用户删除监视的文件夹。我尝试了异步方法的解决方案 http://www.themacaque.com/?p=859 , 但它没有帮助。

提前致谢!

最佳答案

来自 this blog post :

Another potential pitfall of [ReadDirectoryChangesW] is that the referenced directory itself is now "in use" and so can't be deleted. To monitor files in a directory and still allow the directory to be deleted, you would have to monitor the parent directory and its children.

该帖子还提供了有关正确使用 ReadDirectoryChangesW 的更多详细信息

关于python - ReadDirectoryChangesW 阻止删除监视的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6828544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com