Python Changing folder icon in desktop.ini via python

  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Python
AI Thread 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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top