Files changing from UTF-8 to ANSI

305 Views Asked by At

I know a bit about computers, but this one has me puzzled.

Background: I am preparing to teach Python to Middle schoolers in an environment where I can't install Python. I'm trying to use an online IDE with Python built in, and storing .py files locally.

I am creating files at home, saving to a thumb drive, and accessing them at school using my school machine. When I save the files, they are showing as UTF-8, and I can close them and reload, and everything is fine. When I open them on my school machine, they are gobbledygook and are showing as ANSI. I have used several editors, primarily Notepad++ to work with the files. Notepad++ encoding is set to UTF-8, so I'm not sure where the ANSI is coming from. Using the Convert to UTF-8 feature in Notepad++ doesn't seem to do anything. I tried setting the file to read-only before transferring, but that doesn't seem to help. Going back to my home machine, the file is now ANSI and can't be opened on the machine that created it. I've also tried saving the file as a .txt with the same results.

Any insight will be appreciated.

Used Notepad, Notepad++, word, Thonny, Online-Python, Wing, and other editors.

Set Notepad++ Settings...Preferences...New Document to UTF-8 Apply to opened ANSI files

1

There are 1 best solutions below

0
Mark Ransom On

Windows does not record the encoding of a file in any way. It is up to the application opening the file to decide which encoding to use.

Applications such as Notepad try to guess the encoding by looking at part of the file. Sometimes they get it wrong. The problem is that ANSI is always valid because there are no invalid byte values. But as you have noticed, being valid doesn't make it correct.

One convention used often in Windows is to start the file with a byte order mark (BOM) to indicate the file is UTF-8. Both Notepad and Python will recognize this and properly decode your file.