#๐Ÿ”’ configparser spams chinese characters

16 messages ยท Page 1 of 1 (latest)

drifting kestrel
#

I have a script to write to an ini file but whenever i run it, it spams a ton of "random" characters, what could this be caused by?

jade apexBOT
#

@drifting kestrel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

drifting kestrel
#
import os
import configparser

folder = 'C:\\Users\\Yoda\\AppData\\Roaming\\PrismLauncher\\instances'
instances = [name for name in os.listdir(folder) if os.path.isdir(os.path.join(folder, name))]
blacklist = ['.LAUNCHER_TEMP', '.tmp', '1.19.4', '24w13a']
blacklistSet = set(blacklist)

templateCode = "[{name}]\nContainer=MinecraftMenuContainerVisible\nMeter=Image\nImageName={icon}"

for instance in instances:
    instanceInBlacklist = instance in blacklistSet
    if not instanceInBlacklist:
        iconPath = f"C:\\Users\\Yoda\\AppData\\Roaming\\PrismLauncher\\instances\\{instance}\\.minecraft\\icon.png"
        if not os.path.isfile(iconPath):
            iconPath = f"C:\\Users\\Yoda\\AppData\\Roaming\\PrismLauncher\\instances\\{instance}\\minecraft\\icon.png"
            if not os.path.isfile(iconPath):
                iconPath = f"C:\\Users\\Yoda\\AppData\\Roaming\\PrismLauncher\\instances\\{instance}\\icon.png"
                if not os.path.isfile(iconPath):
                    print("Icon not found " + iconPath)
                    iconPath="C:\\Users\\Yoda\\Documents\\Rainmeter\\Skins\\FirstSkin\\@Resources\\Pickaxe64.png"
        write_config = configparser.ConfigParser()
        write_config.add_section(instance)
        write_config.set(instance,"Meter", "Image")
        write_config.set(instance,"Container","MinecraftMenuContainerVisible")
        write_config.set(instance,"ImageName",iconPath)       
        write_config.set(instance,"LeftMouseUpAction",f"[C:\\Users\\Yoda\\AppData\\Local\\Programs\\PrismLauncher\\prismlauncher.exe --launch \"{instance}\"]")

        inifile = open("C:\\Users\\Yoda\\Documents\\Rainmeter\\Skins\\FirstSkin\\Games Menu\\MainMenu.ini", "a")
        write_config.write(inifile)
        inifile.close()
primal current
#

what if you remove the print?

#

and the file is fine itself?

drifting kestrel
#

the chinese characters are from the file.

#

The file (a rainmeter ini) is UTF-16 LE according to notepad

primal current
#

that might be an issue

drifting kestrel
#

i tried just writing with plain text using open, but it had the same issue

primal current
#

so windows uses UTF-16 apparently, but python opens stuff in utf-8

#

try passing encoding="utf-16" to every open() function

drifting kestrel
#

that worked, thanks so much! I tried to do it before but didn't add the encoding= part as i'm not used to python

jade apexBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ configparser spams chinese characters