I have few text files which contain text in Hindi language in a folder. But those text files are in UTF-16 LE
Encoding. I want to change the encoding to UTF-8
without changing text in it. How can I do that?
I wrote two python files but none of them are working proprely. When I run any of them, along with changing the encoding, they clear the file content. These are code in my Python files:
File 1:
import os
for root, dirs, files in os.walk("."):
for filename in files:
#print(filename[-4:])
if(filename[-3:] == "txt"):
f= open(filename,"w+")
x = f.read()
print(x)
f.close()
f1= open(filename, "w+", encoding="utf-8")
f1.write(x)
f1.close()
File 2:
import codecs
BLOCKSIZE = 1048576
with codecs.open("ee.txt", "r", "utf-16-le") as sourceFile:
with codecs.open("ee.txt", "w", "utf-8") as targetFile:
while True:
contents = sourceFile.read(BLOCKSIZE)
print(contents)
if not contents:
break
targetFile.write(contents)