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

Discussion Overview

The discussion revolves around a Python program intended to create a desktop.ini file to change the folder icon on Windows. Participants explore issues related to file formatting and compatibility with Windows' requirements for line endings.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a Python script for creating a desktop.ini file but reports that it is not functioning as expected.
  • Another participant suggests checking for proper line endings, noting that Windows requires \r\n instead of just \n, which is common in Linux.
  • There is a suggestion to verify the functionality of the desktop.ini file by creating it with a standard text editor.
  • A later reply confirms that correcting the line endings resolved the issue, indicating that the original problem was related to file formatting.
  • Another participant mentions the use of text mode ('wt') in Python for handling local end-of-line conventions, suggesting it could be beneficial for cross-platform compatibility.

Areas of Agreement / Disagreement

Participants generally agree that the issue was related to line endings in the desktop.ini file, but there is no consensus on the best practices for handling file formats across different operating systems.

Contextual Notes

Limitations: The discussion does not address potential issues beyond line endings, such as permissions or other attributes that may affect the desktop.ini file's functionality.

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
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K