Changing folder icon in desktop.ini via python

  • Context: Python 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Python
Click For Summary
SUMMARY

The discussion focuses on creating and modifying a desktop.ini file using Python to change folder icons on Windows. The initial code provided by the user failed due to incorrect line endings; Windows requires \r\n instead of \n. After addressing this issue and utilizing text mode ('wt') for file operations, the user successfully implemented the changes. The importance of verifying the desktop.ini file's format and using appropriate line endings is emphasized for successful execution.

PREREQUISITES
  • Understanding of Python file handling and modes
  • Familiarity with Windows file attributes and the attrib command
  • Knowledge of the desktop.ini file structure and its role in Windows
  • Basic understanding of line endings in different operating systems
NEXT STEPS
  • Learn about Python file modes, specifically text mode ('wt')
  • Research the structure and usage of desktop.ini files in Windows
  • Explore the os module in Python for file and directory manipulation
  • Investigate how to programmatically set folder icons using Python on Windows
USEFUL FOR

This discussion is beneficial for Python developers, Windows system administrators, and anyone interested in customizing folder icons programmatically.

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   Reactions: 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...there's 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
 
  • Like
Likes   Reactions: jedishrfu

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 16 ·
Replies
16
Views
7K
  • · Replies 43 ·
2
Replies
43
Views
4K
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