I'm very sorry, but writing a symbol to a text file without saying what the encoding of the file should be is simply non-sense.
It may not be evident at first sight, but text files are indeed encoded and may be encoded in different ways. If you have only letters (upper and lower case, but not accented oned), digits and simple symbols (everything that has an ASCII code below 128), all should be fine, because ASCII 7 bits is now a standard and in fact those characters have same representation in major encodings.
But as soon as you get true symbols, or accented chars, their representation vary from one encoding to the other. For example, the symbol ● has a UTF-8 representation of (Python coding) : \xe2\x97\x8f
. What is worse, it cannot be represented in latin1 (ISO-8859-1) encoding.
Another example is the french e accent aigu : é
it is represented in UTF8 as \xc3\xa9
(note 2 bytes), but is represented in Latin1 as \x89
(one single byte)
So I tested your code in my Ubuntu box using a UTF8 encoding and the command
cat myFile.txt
... correctly showed the bullet !
sba@sba-ubuntu:~/stackoverflow$ cat myFile.txt
●sba@sba-ubuntu:~/stackoverflow$
(as you didn't add any newline after the bullet, the prompt immediately follows it)
In conclusion :
Your code correctly writes the bullet to the file in UTF8 encoding. If your system uses natively another encoding (ISO-8859-1 or its variant Windows-1252) you cannot natively convert it because this character simply does not exist in this encodings.
But you can always see it in a text editor that supports different encoding like the excellent vim that exists on all major systems.
Proof of above :
On a Windows 7 computer, I opened a vim window and instructed it to accept utf8 with :set encoding='utf8'
. I then pasted original code from OP and saved it to a file foo.py
.
I opened a cmd.exe
window and executed python foo.py
(using a Python 2.7) : it created a file myFile.txt
containing the 3 bytes (hexa) : e2 97 8f
that is the utf8 representation of the bullet ●
(I could confirm it with vim Tools/Hexa convert).
I could even open myFile.txt
in idle and actually saw the bullet. Even notepad.exe
could show the bullet !
So even on a Windows 7 computer that does not natively accept utf-8, the code from OP correctly generates a text file that when opened with a text editor accepting UTF-8 contains the bullet ●
.
Of course, if I try to open myFile.txt
with vim in latin1 mode, I get : â—
, on a cmd windows with codepage 850, type myFile.txt
shows ÔùÅ
, and with codepage 1252 (variant of latin1) : â—.
In conclusion original OP code creates a correct utf8 encoded file - it is up to the reading part to interpret correctly utf8.