Python看门狗事件不返回整个src_path [英] Python watchdog event not returning entire src_path

查看:46
本文介绍了Python看门狗事件不返回整个src_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python watchdog 来跟踪本地更改了哪些文件.因为我没有跟踪整个目录,而是跟踪特定文件,所以我使用看门狗的 event.src_path 来检查更改的文件是否是我要查找的文件.

我正在使用 FileSystemEventHandleron_modified,打印 src_path.但是,当我在 gedit 中编辑一个路径为 /home/user/project/test 的文件时,我得到了两个路径,一个看起来像 /home/user/project/.goutputstream-XXXXXX 和一个看起来像这样的:home/user/project/.我从来没有得到我期望的路径.我认为看门狗或我自己的代码可能有问题,但我在 vinano、我的 IDE (PyCharm)、Sublime Text 中测试了完全相同的过程,Atom...他们都给了我我期待的src_path.

我想知道 gedit 是否有解决方法,因为 gedit 是许多 Linux 发行版的默认文本编辑器...提前致谢.

解决方案

来自

无耻地窃取了图像和偏好信息

有关更多信息(以及解决 vim/vi 的具体信息),请参阅 this在看门狗 GitHub 上发布.

基本上对于 Vim,您需要运行这些命令来禁用备份/交换功能:

:set nobackup:set nowritebackup

您可以将它们添加到您的 .vimrc 中以自动执行任务

I'm using python watchdog to keep track of what files have been changed locally. Because I'm not keeping track of an entire directory but specific files, I'm using watchdog's event.src_path to check if the changed file is the one I'm looking for.

I'm using the FileSystemEventHandler and on_modified, printing the src_path. However, when I edit a file that should have the path /home/user/project/test in gedit, I get two paths, one that looks like /home/user/project/.goutputstream-XXXXXX and one that looks something like this: home/user/project/. I never get the path I'm expecting. I thought there may have been something wrong with watchdog or my own code, but I tested the exact same process in vi, nano, my IDE (PyCharm), Sublime Text, Atom...and they all gave me the src_path I'm expecting.

I'm wondering if there is a workaround for gedit, since gedit is the default text editor for many Linux distributions...Thanks in advance.

解决方案

From the Watchdog GitHub readme:

Vim does not modify files unless directed to do so. It creates backup files and then swaps them in to replace the files you are editing on the disk. This means that if you use Vim to edit your files, the on-modified events for those files will not be triggered by watchdog. You may need to configure Vim to appropriately to disable this feature.

As the quote says your issue is due to how these text editors modify files. Basically rather than directly modifying the file, then create "buffer" files that store the edited data. In your case this file is probably .goutputstream-XXXXXX. When you hit save your original file is deleted and a the buffer file is renamed into its place. So your second path is probably the result of the original file being deleted. Sometimes these files serve as backups instead, but still cause similar issues..

By far the easiest method to solve this issue is to disable the weird way of saving in your chosen text editor. In gedit this is done by unchecking the "Create a backup copy of file before saving" option within preferences. This will stop those backup files from being created and simplify life for watchdog. Image and preference info shamelessly stolen from this AskUbuntu question

For more information (and specific information for solving vim/vi) see this issue on the watchdog GitHub.

Basically for Vim you need to run these commands to disable the backup/swapping in feature:

:set nobackup
:set nowritebackup

You can add them to your .vimrc to automate the task

这篇关于Python看门狗事件不返回整个src_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆