0

I have a simple script in Python, in which I am trying to traverse all files in a folder and change them to UTF-8 encoding using Notepad++.

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

However, when I click on Plugins -> Python Script -> Scripts -> MySript, all I get is:

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

When searching the Internet, I have never found anyone with the same problem. All similar problems were caused because people were trying to run the script outside of Notepad++. However, I am getting the error when using Notepad++ plugin directly.

3
  • Where do you think something is assigned to name notepad? Commented Sep 7, 2015 at 9:17
  • 1
    Where is notepad defined? Or console for that matter. Not in the code that you've posted. Commented Sep 7, 2015 at 9:18
  • I supposed it is defined within the plugin itself. The code is just a modification from this post pw999.wordpress.com/2013/08/19/…. The names 'console' or 'notepad' are not defined in the code neither. Commented Sep 7, 2015 at 9:21

1 Answer 1

3

As documented here, editor, notepad, and console are all instances defined within the module itself.

You can prefix those objects with Npp., as you have already done for editor. A less advised option is to do a from Npp import * instead of import Npp.

Do you know the encoding of the source files? If so you can just use Python to convert your files to UTF8 encoding.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I am quite a newbie in Python... to avoid future problems, do I always have to explicitly state the module name, when calling its inner objects?
@Storm: yes that is the preferred way to do it. The reason is that doing an import * can cause problems if there are names in the imported file that clash with those of the importing module, or other imported modules. E.g. if you had a function named max() in module m and you did an import * from m the max() from m would hide the built-in max() function. For that reason it is preferable to import the module, or import specific names from the module, e.g. from Npp import editor, console, notepad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.