[pygtk] Working with files with python (gtk)
Ed Catmur
ed at catmur.co.uk
Sat Mar 17 22:26:40 WST 2007
On Thu, 2007-03-15 at 15:28 -0400, Edward A Robinson wrote:
> This is my first time posting to the python tutor so be nice, I have
> read the mailing list and cant seem to find and answer there or on
> google so I turn to you guys for help. I'm working on a gtk
> application and I want to list all the image files in a directory. Now
> I know that if I have an image lets say foo.png and I rename that to
> foo.txt nautilus (the default gnome browser) still recognizes that
> file as an image.How can I do the same thing?
>
> I guess what Im looking for is a python command that dose what the
> file command in the shell would do, as well as figure out the correct
> program to open the file with.
Use the gnomevfs module (in gnome-python, probably). It's not that well
documented; you probably want gnomevfs.get_mime_type().
> As well as recognizes images I want users to be able to open up each
> image with that images default editor. So foo.png may open with eye of
> gnome but bar.jpg may open with gimp. (If those where the settings in
> nautilus)
Use gnomevfs.mime_get_default_application(). For example:
import gnomevfs, gobject
def open_uri(uri):
mime = gnomevfs.get_mime_type (uri)
scheme = gnomevfs.get_uri_scheme (uri)
# http://bugzilla.gnome.org/show_bug.cgi?id=411560
id, name, command, multi, paths_for_local, schemes, term = gnomevfs.mime_get_default_application (mime)
if scheme == 'file' and paths_for_local:
return gobject.spawn_async ([command, gnomevfs.get_local_path_from_uri (uri)], flags=gobject.SPAWN_SEARCH_PATH)
elif scheme == 'file' or scheme in schemes:
return gobject.spawn_async ([command, uri], flags=gobject.SPAWN_SEARCH_PATH)
else:
for id, name, command, multi, paths_for_local, schemes, term in gnomevfs.mime_get_short_list_applications (mime) + gnomevfs.mime_get_all_applications (mime):
if scheme in schemes:
return gobject.spawn_async ([command, uri], flags=gobject.SPAWN_SEARCH_PATH)
return False
open_uri ("file:///path/to/file")
> Also how would I get cut and paste to work so that I can cut a file
> from my image browser then paste it into nautilus. I know how to get
> it to work for basic text but not for files.
Use gtk.Clipboard.set_with_data with
targets=gtk.target_list_add_uri_targets(); see gtk.SelectionData.
Ed
More information about the pygtk
mailing list