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

标签 python linux character-encoding

我设置了 iwatch 来监控一些目录。我不想给我发电子邮件,而是想执行自己的脚本。一切顺利,直到出现一些特殊字符。

iwatch -f iwatch.xml iwatch.xml 在哪里

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

提交程序:

#!/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

所以,当我运行“commit ÄÖÜäöüß”时,一切都很好

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

但是,当它从 iwatch 被触发时(触摸 ÄÖÜäöüß),我明白了

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

我对编码不太了解:/

如何在此处获取我的可读内容?

我已经尝试了我能找到的所有方法(以千种方式编码/解码),但没有弄清楚。有人可以帮我吗?每一个建议将不胜感激!非常感谢!

最佳答案

新:

这个家伙为 iwatch 写了一个补丁。

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

两个提供的补丁都对我有用。

补丁 2 给出了 ...pragma is deprecated... 警告。

旧的:

更改策略,我将围绕 pyinotify 进行重建。这很好用!

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()

关于python - (Linux) iwatch 在文件名中使用特殊字符执行 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34578048/

相关文章:

python - 在 OS X 10.7 上为 Python 3.2 安装 PyCrypto

python - python SMTP 未收到邮件

linux - 函数中的命令不能像在终端中手动输入那样工作

c - 在 C 中的 Linux 上的 $PATH 中搜索文件

ruby - 正则表达式错误 : too many multibyte code ranges are specified

python - 我不明白为什么我的 if 循环不能正常工作

python - 重新创建字符级RNN以生成文本

linux - Apt-get 反复失败。身份验证警告和 404 错误

c# - 在 C# 中的自定义代码表中将阿拉伯语转换为十六进制的最佳方法

javascript - 作为纯文本加载的脚本 - 未声明纯文本文档的字符编码。