1

For some reason, whenever I try to append text to a file, a bunch of -what I believe are- Asian characters get appended instead.

Here's my code:

with open(rutaCuentasCFG+'cuentas.cfg', 'r') as file:
    contFile = file.readlines()

cantidadCuentas = len(contFile)

with open('C:\Sandboxie.ini', 'a', encoding='utf-8') as file:
    for cont in range(1,len(contFile)+1):
        config = ['\n','\n'+'[steam'+str(cont)+']'+'\n',
                  '\n',
                  'Enabled=y'+'\n',
                  'ConfigLevel=7'+'\n',
                  'AutoRecover=y'+'\n',
                  'Template=BlockPorts'+'\n',
                  'Template=LingerPrograms'+'\n',
                  'Template=Firefox_Phishing_DirectAccess'+'\n',
                  'Template=AutoRecoverIgnore'+'\n',
                  'RecoverFolder=%{374DE290-123F-4565-9164-39C4925E467B}%'+'\n',
                  'RecoverFolder=%Personal%'+'\n',
                  'RecoverFolder=%Favorites%'+'\n',
                  'RecoverFolder=%Desktop%'+'\n',
                  'BorderColor=#00FFFF,ttl'+'\n',
                  'OpenPipePath='+rutaSteamIdle+'\\SteamIdle\\']
        for line in config:
            file.write(line)

This is the result I'm getting: http://puu.sh/4f6y5.png

And this is the one I should get: http://puu.sh/4f6CE.png

I also tried without specifying the encoding with the same results.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • What is the encoding of the original file? Open it with Notepad and see what the default encoding is when you "Save As". – tom Aug 30 '13 at 23:54
  • It appears to be Unicode – user2020618 Aug 30 '13 at 23:58
  • 1
    Apparently [Notepad's "Unicode" means UTF-16](http://stackoverflow.com/questions/3710374/get-encoding-of-a-file-in-windows). Try using that as the Python encoding. – tom Aug 31 '13 at 00:08
  • Glad it helped. I posted my comments as an answer. – tom Aug 31 '13 at 01:02

1 Answers1

1

The original file is in a different encoding. Change your code to use that encoding.

To find the encoding open the file in Notepad, click File > Save As... and note the default encoding. "Unicode" means UTF-16.

tom
  • 21,844
  • 6
  • 43
  • 36