繁体   English   中英

Notepad ++ Python脚本名称'notepad'未定义

[英]Notepad++ Python script name 'notepad' is not defined

我在Python中有一个简单的脚本,其中试图遍历文件夹中的所有文件,然后使用Notepad ++将其更改为UTF-8编码。

import os;
import sys;
import Npp;
Npp.editor.write('Starting\r\n')
filePathSrc="C:\\SomePath"
Npp.editor.write('FilePath: ' + str(filePathSrc) + '\r\n')
try:
    for root, dirs, files in os.walk(filePathSrc):
        for fn in files:
            if fn[-4:] == '.txt' or fn[-4:] == '.xml' or fn[-5:] == '.html':
                notepad.open(root + "\\" + fn)
                console.write(root + "\\" + fn + "\r\n")
                notepad.runMenuCommand("Encoding", "Encode in UTF-8")
                notepad.save()
                notepad.close()
                Npp.editor.write('Converted ' + str(fn))
except Exception as ex:
    Npp.editor.write('Error occured\r\n')
    Npp.editor.write(str(ex))

但是,当我单击插件-> Python脚本->脚本-> MySript时,我得到的只是:

Starting
FilePath: C:\SomePath
Error occured
name 'notepad' is not defined

在Internet上搜索时,我从未发现有相同问题的人。 引起所有类似的问题的原因是人们试图在Notepad ++之外运行脚本。 但是,直接使用Notepad ++插件时出现错误。

如此处所述editornotepadconsole都是模块本身内定义的所有实例。

您可以为这些对象添加Npp.前缀Npp. ,就像您已经为editor所做的那样。 一个不太建议的选项是执行from Npp import *而不是import Npp

您知道源文件的编码吗? 如果是这样,您可以使用Python将文件转换为UTF8编码。

暂无
暂无

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

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