Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the full path from a GFile
#1
Development on Windows 11 using python 3 in Gimp 3.0.6.

I can find no way of getting the path from a GFile returned from the parameter setting dialog in a plugin.

A file parameter is created by
Code:
procedure.add_file_argument("src-folder",
                "Source folder",
                "Location of files to parse",
                Gimp.FileChooserAction.CREATE_FOLDER,
                False,
                None,
                GObject.ParamFlags.READWRITE)
The purpose of this is to allow browsing for a directory. It shows up in the plugin dialog as a directory browser, and a directory can be selected.

The parameter is then obtained after the dialog has run as
Code:
src_gfile = config.get_property("src-folder")
This has returned a GFile, not (unfortunately) a string path. The question is, how do I get the path out of the GFile? (I need it because I need to do some manipulations on it).

I've tried various things - src_gfile.get_path() produced nothing (though in another plugin I looked at it clearly did looking at the code, but that may have been on Linux, not Windows). I did find that if in the python console I create a GFile and get the path as
Code:
file = Gio.File.new_for_path("C:\\a\\b\\c")
file.get_path()
I do get the path as 'C:\a\b\c' - but not when it comes from the config as above.

src_gfile.get_uri() does produce the URI as a string, looking something line 'file:\\\C:\a\b\c'. Being a URI, it has the scheme on the from, and I can lose the 'file:\\\' by editing the string, but can't help feeling that is a bodge, certainly not cross platform.

I then found query_info(). So I tried
Code:
file_info = src_gfile.query_info("standard::*", Gio.FileQueryInfoFlags.NONE, None)
file_info.get_display_name()
I do get something back - but it's only the final leaf, ie 'c', not the full path and I can't find any method that does that.

So, basically, I can find no way of tidily get the full path as a string from a GFile. Can anyone help?
Reply
#2
Would this help? https://docs.gtk.org/gio/method.File.get_path.html
Reply
#3
Thanks, yes, that's the function I used. In some circumstances it works, as when I created the GFile with new,_from_path(), but not when the GFile is returned from config.get_property(). The help says sometimes it can return a path, sometimes not. Still looking, can't believe it can be this obscure, I must have missed something.
Reply
#4
Maybe this helps you:

Code:
# filename howto:
#  import os.path
#  dirname = '/foo/bar'
#  filename = 'abc.txt'
#  os.path.join(dirname, filename)

file = os.path.join(dir, filename)

# Convert filename to GFile
gfile = Gio.File.new_for_path(file)
Reply
#5
Yes thanks, but actually the path is not created by me, it's created by the dialog that is automatically built by Gimp for setting the parameters for the plugin. One in my case is a directory browser to allow selecting a directory to save images to. Then I get the path from the parameter stored in the config after the dialog has run. But, it returns a GFile not a path string, I'm trying to get the path string out of the GFile as I need to manipulate the path. The GFile must know what the path is - but it won't tell me.
Reply


Forum Jump: