-1

I would like to change encoding of .txt files in a directory with python to UTF-8, are there ways to do that ?

Thank you for your support.

I have viewed the solution already mentionned by stackoverflow users here : How to convert a file to utf-8 in Python?

I would like to apply it for all files of particular category in the directory and not one file.

import codecs
BLOCKSIZE = 1048576 # or some other, desired size in bytes
with codecs.open(sourceFileName, "r", "your-source-encoding") as sourceFile:
    with codecs.open(targetFileName, "w", "utf-8") as targetFile:
        while True:
            contents = sourceFile.read(BLOCKSIZE)
            if not contents:
                break
            targetFile.write(contents)

1) I would like to change encoding of files in a directory to UTF-8, I know the input encoding.

2) are there solutions to transform to UTF-8 without knowing the input encoding ? ( not important at this time, but if a solution already exist, it will be great to know about it)

HappyMan
  • 75
  • 9

1 Answers1

0

Put the below line above with codecs.open(sourceFileName, "r", "your-source-encoding") as sourceFile line of code:

for sourceFileName in os.listdir("./Your_File_path"):

If you want to do only .txt files and in your path their are other files also..do it by glob

import glob
for filename in glob.glob('*.txt'):
Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51