Python Changing folder icon in desktop.ini via python

  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Python
Click For Summary
A Python program was created to generate a desktop.ini file in a specified folder and change its icon, but it initially failed to work. Key issues identified included the use of line endings; the program used "\n" which is suitable for Linux, while Windows requires "\r\n" for proper line breaks. The discussion highlighted the importance of verifying the desktop.ini file's format and suggested using a text editor to confirm functionality. Ultimately, the program worked after addressing the line ending issue. The conversation emphasized the utility of text mode ('wt') for handling local end-of-line conventions in file operations, particularly in Windows environments.
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have created this python program to create a desktop.ini file in a test folder and then change the icon of it. However, the program is something like this

Code:
import os

desktop_ini = ["[.ShellClassInfo]\n",
     "IconResource=C:\\Users\\Arman\\Desktop\\Coding\\Desktop Icons\\directory_closed-5.ico,0\n",
     'FolderType=Generic']with open(r'C:\Users\Arman\Desktop\TestFolder\desktop.ini', 'w') as f:
    f.writelines(desktop_ini)
    f.close()os.system('attrib +s +h C:\\Users\\Arman\\Desktop\\TestFolder\\desktop.ini')
os.system('attrib +r C:\\Users\\Arman\\Desktop\\TestFolder')

but it's not working
 
Technology news on Phys.org
I would check the desktop.ini file to see if there's a CR LF after every line and that there is nothing more added to the file like blank lines...

In you program, you write a \n which is fine for linux but windows requires \r\n after lines.

Also does the writelines add a LF to the file?

Have you tried creating the desktop.ini file with an ordinary editor to verify that it works?
 
  • Like
Likes Arman777 and sysprog
jedishrfu said:
Have you tried creating the desktop.ini file with an ordinary editor to verify that it works?
It did not worked...Theres something wrong but I don't know what
 
jedishrfu said:
In you program, you write a \n which is fine for linux but windows requires \r\n after lines.
wait. It worked. Thanks a lot
 
For "transparent" handling of the local end-of-line convention when opening a file, text mode, i.e. 'wt' in your case, can be a useful option. In your case the program sounds like its specific for Windows making this less of an issue, but in other cases where the local OS doesn't matter text mode is a good choice for textual input/output. See more at https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 16 ·
Replies
16
Views
6K
  • · Replies 43 ·
2
Replies
43
Views
3K
Replies
12
Views
2K
  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K