Notepad ++转换为UTF-8多个文件

时间:2016-02-20 17:17:46

标签: python utf-8 console notepad++

Notepad ++的“转换为无BOM的UTF-8”功能非常好。但我有200个文件,所有这些文件都需要被隐藏。因此我找到了这个小小的python脚本:

import os;
import sys;
filePathSrc="C:\\Temp\\UTF8"
for root, dirs, files in os.walk(filePathSrc):
    for fn in files:
      if fn[-4:] != '.jar' and fn[-5:] != '.ear' and fn[-4:] != '.gif' and fn[-4:] != '.jpg' and fn[-5:] != '.jpeg' and fn[-4:] != '.xls' and fn[-4:] != '.GIF' and fn[-4:] != '.JPG' and fn[-5:] != '.JPEG' and fn[-4:] != '.XLS' and fn[-4:] != '.PNG' and fn[-4:] != '.png' and fn[-4:] != '.cab' and fn[-4:] != '.CAB' and fn[-4:] != '.ico':
        notepad.open(root + "\\" + fn)
        console.write(root + "\\" + fn + "\r\n")
        notepad.runMenuCommand("Encoding", "Convert to UTF-8 without BOM")
        notepad.save()
        notepad.close()

它遍历每个文件 - >我可以看到这个。但在它完成后,charset在我的情况下是stil ANSI:/

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:4)

弄错了。我的记事本是德语。因此,如果它被称为“编码”或在我的情况下称为“Kodierung”和“转换为没有BOM的UTF-8”,请注意“Konvertiere zu UTF-8 ohne BOM”

这帮助了我!

答案 1 :(得分:4)

这对我有用:

转到 Notepad ++->插件->插件管理

找到并安装 Python脚本插件。

使用插件-> Python脚本->新脚本创建新的python脚本。

将此代码插入脚本:

import os;
import sys;
filePathSrc="C:\\Users\\YourUsername\\Desktop\\txtFolder"
for root, dirs, files in os.walk(filePathSrc):
    for fn in files:
      if fn[-4:] == '.txt' or fn[-4:] == '.csv':
        notepad.open(root + "\\" + fn)
        console.write(root + "\\" + fn + "\r\n")
        notepad.runMenuCommand("Encoding", "Convert to UTF-8")
        notepad.save()
        notepad.close()

C:\\Users\\YourUsername\\Desktop\\txtFolder替换为文件所在Windows文件夹的路径。

脚本可处理.txt.csv文件,并忽略文件夹中的所有其他文件。

使用插件-> Python脚本->脚本->脚本名称

运行脚本