Changing folder icon in desktop.ini via python

  • Python
  • Thread starter Arman777
  • Start date
  • Tags
    Python
In summary, the conversation discusses a python program that creates a desktop.ini file in a test folder and changes its icon. The program is not working due to incorrect line endings in the file, and the individual suggests using 'text mode' as a solution.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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
  • #2
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
  • #3
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
 
  • #4
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
 
  • #5
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 jedishrfu

FAQ: Changing folder icon in desktop.ini via python

How can I change the folder icon using python?

To change the folder icon using python, you can use the desktop.ini file, which is a hidden configuration file that stores information about the folder's appearance. By modifying this file, you can change the folder icon through python code.

What is the process for changing the folder icon using python?

The process for changing the folder icon using python involves creating a desktop.ini file in the specified folder and writing code to modify the IconResource field in the file. This field contains the path to the icon that will be used for the folder.

Can I change the folder icon for multiple folders at once using python?

Yes, you can change the folder icon for multiple folders at once using python. You can use a loop to iterate through a list of folders and modify the desktop.ini file for each folder with the desired icon.

How can I ensure that the folder icon change persists even after restarting my computer?

To ensure that the folder icon change persists even after restarting your computer, you need to make sure that the desktop.ini file is set to be hidden and read-only. This will prevent the system from overwriting the changes when the folder is accessed.

Are there any limitations to changing folder icon using python?

Yes, there are some limitations to changing folder icon using python. The folder must have a desktop.ini file already present, and the icon file must be in the .ico format. Additionally, this method may not work on all operating systems or with all types of folders.

Similar threads

Replies
16
Views
5K
Replies
9
Views
9K
Replies
3
Views
3K
Replies
9
Views
1K
Replies
4
Views
3K
Replies
3
Views
693
Back
Top