[Python] Python creating a desktop.ini file for custom folder icon

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python program that converts jpegs into icon files and creates a desktop.ini file for the parent folder. For the most part it works. If I've already modified the folder properties, the icon displays correctly because the desktop.ini file was already created by the system. But if I hadn't modified the folder properties manually, and therefore there isn't a desktop.ini file yet, I can get my script to create a file named desktop.ini, but it shows up instead of being a hidden file and the folder doesn't sync with it until I go into it's properties myself (at which point it can kind of recognize that the configuration file is there).

    It's part of a larger project, but here is the relevant code.

    icon = Image.open(f'{directory}/{jpegs[0]}')
    icon = icon.resize((256,256))
    icon.save(f'{directory}/{group_name}.ico')
    config = configparser.ConfigParser()
    config.read(f'{directory}/desktop.ini')
    if not config.has_section('ViewState'):
    config.add_section('ViewState')
    config['ViewState']['Mode'] = ''
    config['ViewState']['Vid'] = ''
    config['ViewState']['FolderType'] = 'Pictures'
    if not config.has_section('.ShellClassInfo'):
    config.add_section('.ShellClassInfo')
    config['.ShellClassInfo']['IconResource'] = f'{os.path.abspath(f'{directory}/{group_name}.ico')},0'
    try:
    with open(f'{directory}/desktop.ini', 'r+') as f:
    config.write(f)
    except Exception as e:
    print(e)


    Is there a way to create a new desktop.ini file, that specifies the folder icon, and has the file explorer recognize the connection?

    Edit: I have the file opened as 'r+' because if I do 'w' or 'w+' it doesn't work with permissions on the system created desktop.ini files. Is it perhaps that I need to create the files with special metadata and permissions?

    Continue reading...

Compartilhe esta Página