Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
How can I change the folder icon?

I'm using python. I want change a file icon with Gio from a python app. I found a lot of deprecated documentation and I'm not sure how I can do it.

I tried this, but nothing happen:

from gi.repository import Gio
f = Gio.File.parse_name('/home/my_user/Desktop/test.txt')
f.set_attribute_string("metadata::custom_icon", '/usr/share/icons/Humanity/animations/48/brasero-disc-100.svg', Gio.FileQueryInfoFlags.NONE, None)

Which class and method have I use? Any idea, please? Thanks in advance!

share|improve this question

marked as duplicate by dobey, Eliah Kagan, Jorge Castro, Ringtail, Uri Herrera Sep 14 '12 at 5:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

I think is not a duplicate, because I don't want use 'gvfs-set-attribute', because it's not refresh nautilus contents. By this reason I want use Gio :) – costales Sep 13 '12 at 15:35
gvfs-set-attribute is merely a command line tool which does basically the exact same thing you are doing in your code. You can use it as a quick way to test, rather than editing the code. – dobey Sep 13 '12 at 17:04
up vote 2 down vote accepted

This is mostly a duplicate of a previous question I answered, but you have 2 problems with your current code:

  • It is custom-icon and not custom_icon
  • You really do not want to set the icon of a file to be a file from the animations context of a theme. Or in general, a file at all. It's better to set the custom-icon-name attribute if you really must, and then have that icon be a part of the theme, so that it works correctly if the user changes themes.
share|improve this answer
Thanks dobey! but I tried 'custom-icon' and 'custom-icon-name', and Nautilus is not changing the icon :( Any idea? Thanks in advance! :) – costales Sep 13 '12 at 15:29
You need to set custom-icon-name to just the icon name, such as "text-x-generic" for it to work. You may also need to refresh the view in nautilus, if you have the window already open and expect it to change immediately. – dobey Sep 13 '12 at 16:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.