简体   繁体   English

(Linux)iwatch使用文件名中的特殊字符执行python

[英](Linux) iwatch executing python with special chars in filename

I've set up iwatch to monitor some directory. 我已经设置了iwatch来监视某些目录。 Instead off emailing me, i want to execute my own script. 而不是通过电子邮件发送给我,我想执行自己的脚本。 Everything went well, until some special characters showed up. 一切顺利,直到出现一些特殊字符为止。

iwatch -f iwatch.xml where iwatch.xml is iwatch -f iwatch.xml,其中iwatch.xml是

[...]
<path type="recursive" alert="off" events="close_write" exec="commit '%f'">/user/Desktop/test</path>
[...]

commit programm: 提交程序:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import chardet,sys

print 40*"-" 
print chardet.detect(sys.argv[1])
print type(sys.argv[1]), sys.argv[1]
uni = unicode(sys.argv[1], sys.getfilesystemencoding())
print type(uni), uni

So, when i run "commit ÄÖÜäöüß", everything is fine 因此,当我运行“ commitÄÖÜäöüß”时,一切都很好

----------------------------------------
{'confidence': 0.99, 'encoding': 'utf-8'}
<type 'str'> ÄÖÜäöüß
<type 'unicode'> ÄÖÜäöüß

But, when it get fired from iwatch (touch ÄÖÜäöüß), i get 但是,当它从iwatch(触摸ÄÖÜäöüß)发射时,我得到了

----------------------------------------
{'confidence': 0.99, 'encoding': 'utf-8'}
<type 'str'> /root/Desktop/test/ÃÃÃäöüÃ
<type 'unicode'> /root/Desktop/test/ÃÃÃäöüÃ

I don't know so much about encoding :/ 我对编码不是很了解:/

How do I get my readable stuff here? 我如何在这里获得可读性?

I've tried everything I could find (encoding/decoding in thousand ways) but didn't figuered it out. 我已经尝试了所有可能的方法(以千种方式进行编码/解码),但并未弄清楚。 Can somebody help me? 有人可以帮我吗? Every suggest would be appreciated! 每个建议将不胜感激! Thank you very much! 非常感谢你!

New: 新:

This dude wrote a patch for iwatch. 这个家伙为iwatch编写了一个补丁。

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=608843 https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=608843

Both provided patches are working for me. 提供的两个补丁都对我有用。

Patch 2 gives a ...pragma is deprecated... warning. 补丁2给出了...不建议使用杂项...警告。

Old: 旧:

Changed strategy, I'll rebuild around pyinotify. 更改策略后,我将围绕pyinotify进行重建。 This works great! 这很棒!

import pyinotify

wm = pyinotify.WatchManager()

mask = pyinotify.IN_CLOSE_WRITE

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_CLOSE_WRITE(self, event):
        print "Wrote:", event.pathname

handler = EventHandler()

notifier = pyinotify.Notifier(wm, handler)

wdd = wm.add_watch('/root/Desktop/test', mask, rec=True)

notifier.loop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM