<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Python: Multiple files ANSI to utf-8 converter]]></title><description><![CDATA[<p dir="auto">hello, I want to use “Python Script” Plugin as to convert multiple files to UTF-8 (not UTF-8-BOM), on a particular folder.</p>
<p dir="auto">Can be this done?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/24214/python-multiple-files-ansi-to-utf-8-converter</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Jul 2026 22:45:59 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/24214.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 06 Mar 2023 04:07:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Python: Multiple files ANSI to utf-8 converter on Tue, 07 Mar 2023 21:51:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: Mark-Olson">@<bdi>Mark-Olson</bdi></a> ,</p>
<blockquote>
<p dir="auto">Note that this doesn’t use Notepad++ for anything,</p>
</blockquote>
<p dir="auto">I appreciate your willingness to help.  However, we need to focus this Forum on Notepad++.  If it’s something that can be done in PythonScript, and you are interested in providing the solution, please make it compatible with PythonScript.  This forum isn’t for “generic” Python code-writing.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84678</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84678</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 07 Mar 2023 21:51:21 GMT</pubDate></item><item><title><![CDATA[Reply to Python: Multiple files ANSI to utf-8 converter on Tue, 07 Mar 2023 21:45:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a> yes, sure, I will use chat. thanks</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84677</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84677</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Tue, 07 Mar 2023 21:45:49 GMT</pubDate></item><item><title><![CDATA[Reply to Python: Multiple files ANSI to utf-8 converter on Tue, 07 Mar 2023 21:43:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: Hellena-Crainicu">@<bdi>Hellena-Crainicu</bdi></a></p>
<p dir="auto">This topic has delved into off-topic land for Notepad++ discussion.  I doubt anybody wants to debug your code, but on the offhand chance that Mark does, why don’t you two take this discussion off into a private chat?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84676</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84676</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 07 Mar 2023 21:43:59 GMT</pubDate></item><item><title><![CDATA[Reply to Python: Multiple files ANSI to utf-8 converter on Tue, 07 Mar 2023 21:34:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: Mark-Olson">@<bdi>Mark-Olson</bdi></a> Mark Olson:  please check my code, and the replacements I made, and tell me what is wrong.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84675</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84675</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Tue, 07 Mar 2023 21:34:52 GMT</pubDate></item><item><title><![CDATA[Reply to Python: Multiple files ANSI to utf-8 converter on Tue, 07 Mar 2023 21:28:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: Mark-Olson">@<bdi>Mark-Olson</bdi></a> I still have a problem. I change everything on your code, as I post yesterday. I run again today, but I get thie error.</p>
<p dir="auto"><img src="/assets/uploads/files/1678224227052-77176334-bdea-4bd2-9579-1d1e7ec8f7b3-image.png" alt="77176334-bdea-4bd2-9579-1d1e7ec8f7b3-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">So, this is the code I run today, trying to change txt files from UTF-8-BOM to UTF-8.  The error above. Doesn’t work the conversion. Why ? I put the dir name, the encoding, etc…</p>
<pre><code>import os
import glob

def change_encoding(fname, from_encoding, to_encoding='utf-8') -&gt; None:
    '''
    Read the file at path fname with its original encoding (from_encoding)
    and rewrites it with to_encoding.
    '''
    with open(fname, encoding=from_encoding) as f:
        text = f.read()
    with open(fname, 'w', encoding=to_encoding) as f:
        f.write(text)


if __name__ == '__main__':
    import argparse
    import glob  # pip install glob2
    parser = argparse.ArgumentParser()
    parser.add_argument('dirname',
        help='d:\\2022_12_02\\word 2\\1')  # name of directory in which you want to change file encodings
    parser.add_argument('old_encoding',
        help='UTF-8-BOM') # the previous encoding of files found ANSI
    parser.add_argument('new_encoding', nargs='?', default='utf-8',
        help='UTF-8')
    parser.add_argument('include_files', nargs='*',
        help='*')  # filename patterns using glob syntax to choose
    args = parser.parse_args()
    include_files = args.include_files
    if not include_files:
        include_files = ['*.*']
    fnames = set()
    curdir = os.getcwd()
    try:
        os.chdir(args.dirname)
        for glb in include_files:
            fnames.update(glob.glob(glb))
        for fname in fnames:
            print((f'changing encoding of {fname} from '
                  f'{args.old_encoding} to {args.new_encoding}'))
            change_encoding(fname, args.old_encoding, args.new_encoding)
    finally:
        os.chdir(curdir)
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/84674</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84674</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Tue, 07 Mar 2023 21:28:03 GMT</pubDate></item><item><title><![CDATA[Reply to Python: Multiple files ANSI to utf-8 converter on Mon, 06 Mar 2023 18:30:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: Hellena-Crainicu">@<bdi>Hellena-Crainicu</bdi></a><br />
Correct, the intended use of the script (and pretty much any Python script with the line <code>import argparse</code> in it) is not to be modified directly, but rather to be used from the command line with arguments. The changes you made don’t alter the functionality at all, but rather change the help message displayed.</p>
<p dir="auto">I’ll just repeat the usage examples in my docstring at the beginning of the script.</p>
<pre><code class="language-sh">&gt;python -m encoding_conversion . utf-16 utf-8 *.txt
&gt;python -m encoding_conversion "example directory" utf-8 utf-16 *.md
</code></pre>
<p dir="auto">The former changes all utf-16 encoded text files to utf-8, the latter changes all utf-8 encoded markdown (<code>.md</code>) files to utf-16.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84623</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84623</guid><dc:creator><![CDATA[Mark Olson]]></dc:creator><pubDate>Mon, 06 Mar 2023 18:30:07 GMT</pubDate></item><item><title><![CDATA[Reply to Python: Multiple files ANSI to utf-8 converter on Mon, 06 Mar 2023 18:03:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: Mark-Olson">@<bdi>Mark-Olson</bdi></a> said in <a href="/post/84620">Python: Multiple files ANSI to utf-8 converter</a>:</p>
<p dir="auto"><strong>Ok, I change the lines.  If I understand well enough:</strong></p>
<p dir="auto"><code>help='d:\\2022_12_02\\word 2\\1')  # name of directory in which you want to change file encodings</code></p>
<p dir="auto"><code>help='ANSI') # the previous encoding of files found</code></p>
<p dir="auto"><code>help='*.txt')  # filename patterns using glob syntax to choose</code></p>
<p dir="auto">Ok, I run the code in Python directly. Nothing happens…</p>
<pre><code>import os

def change_encoding(fname, from_encoding, to_encoding='utf-8') -&gt; None:
    '''
    Read the file at path fname with its original encoding (from_encoding)
    and rewrites it with to_encoding.
    '''
    with open(fname, encoding=from_encoding) as f:
        text = f.read()
    with open(fname, 'w', encoding=to_encoding) as f:
        f.write(text)


if __name__ == '__main__':
    import argparse
    import glob
    parser = argparse.ArgumentParser()
    parser.add_argument('dirname',
        help='d:\\2022_12_02\\word 2\\1')  # name of directory in which you want to change file encodings
    parser.add_argument('old_encoding',
        help='ANSI') # the previous encoding of files found
    parser.add_argument('new_encoding', nargs='?', default='utf-8',
        help='UTF-8')
    parser.add_argument('include_files', nargs='*',
        help='*.txt')  # filename patterns using glob syntax to choose
    args = parser.parse_args()
    include_files = args.include_files
    if not include_files:
        include_files = ['*.*']
    fnames = set()
    curdir = os.getcwd()
    try:
        os.chdir(args.dirname)
        for glb in include_files:
            fnames.update(glob.glob(glb))
        for fname in fnames:
            print((f'changing encoding of {fname} from '
                  f'{args.old_encoding} to {args.new_encoding}'))
            change_encoding(fname, args.old_encoding, args.new_encoding)
    finally:
        os.chdir(curdir)
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/84622</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84622</guid><dc:creator><![CDATA[Hellena Crainicu]]></dc:creator><pubDate>Mon, 06 Mar 2023 18:03:25 GMT</pubDate></item><item><title><![CDATA[Reply to Python: Multiple files ANSI to utf-8 converter on Mon, 06 Mar 2023 17:17:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hellena-crainicu" aria-label="Profile: Hellena-Crainicu">@<bdi>Hellena-Crainicu</bdi></a><br />
This script (not a PythonScript plugin script, because that’s not really the most effective solution) should do what you want:</p>
<pre><code class="language-py">'''
This should be used as a script from the terminal.
Relevant documentation:
* https://docs.python.org/3/howto/unicode.html
* https://docs.python.org/3/library/glob.html
* https://docs.python.org/3/library/os.html#module-os
* https://docs.python.org/3/library/argparse.html
example usage:
&gt;python -m encoding_conversion . utf-16 utf-8 *.txt
changing encoding of example2.txt from utf-16 to utf-8
changing encoding of example4.txt from utf-16 to utf-8
changing encoding of example3.txt from utf-16 to utf-8
changing encoding of example1.txt from utf-16 to utf-8
changing encoding of example5.txt from utf-16 to utf-8
&gt;python -m encoding_conversion "example directory" utf-8 utf-16 *.md
changing encoding of new 2.md from utf-8 to utf-16
changing encoding of new 3.md from utf-8 to utf-16
changing encoding of new 1.md from utf-8 to utf-16
'''
import os

def change_encoding(fname, from_encoding, to_encoding='utf-8') -&gt; None:
    '''
    Read the file at path fname with its original encoding (from_encoding)
    and rewrites it with to_encoding.
    '''
    with open(fname, encoding=from_encoding) as f:
        text = f.read()
    with open(fname, 'w', encoding=to_encoding) as f:
        f.write(text)


if __name__ == '__main__':
    import argparse
    import glob
    parser = argparse.ArgumentParser()
    parser.add_argument('dirname',
        help='name of directory in which you want to change file encodings')
    parser.add_argument('old_encoding',
        help='the previous encoding of files found')
    parser.add_argument('new_encoding', nargs='?', default='utf-8',
        help='the new encoding that you want to change to')
    parser.add_argument('include_files', nargs='*',
        help='filename patterns using glob syntax to choose')
    args = parser.parse_args()
    include_files = args.include_files
    if not include_files:
        include_files = ['*.*']
    fnames = set()
    curdir = os.getcwd()
    try:
        os.chdir(args.dirname)
        for glb in include_files:
            fnames.update(glob.glob(glb))
        for fname in fnames:
            print((f'changing encoding of {fname} from '
                  f'{args.old_encoding} to {args.new_encoding}'))
            change_encoding(fname, args.old_encoding, args.new_encoding)
    finally:
        os.chdir(curdir)
</code></pre>
<p dir="auto">Note that this doesn’t use Notepad++ for anything, because it is simpler to get the job done with pure Python.</p>
<p dir="auto">You could probably modify this script to try to guess the encoding of files, but I’ve tried using automatic encoding detection in Python and it’s pretty hit-or-miss. If you’re really determined to try guessing encoding, try looking at <a href="https://docs.python.org/3/library/codecs.html#module-codecs" rel="nofollow ugc">codecs</a>.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84620</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84620</guid><dc:creator><![CDATA[Mark Olson]]></dc:creator><pubDate>Mon, 06 Mar 2023 17:17:52 GMT</pubDate></item></channel></rss>