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
#6
What about:

Code:
Gimp.file_get_utf8_name(gfile)


gfile as from the example above.
Reply
#7
Thanks. Odd result again.

If I use that in the python console on a GFile I create with ...new_for_path(<path>), it's fine, I get the path back, but if I use it on the GFile that comes out of the config, ie set by the dialog, I get file:\\\<path> - same therefore as gf.get_uri(). Hey ho.
Reply
#8
(01-21-2026, 10:46 PM)nmw01223 Wrote: Thanks. Odd result again.

If I use that in the python console on a GFile I create with ...new_for_path(<path>), it's fine, I get the path back, but if I use it on the GFile that comes out of the config, ie set by the dialog, I get file:\\\<path> - same therefore as gf.get_uri(). Hey ho.

As a quick and dirty test I 'mangled' the save histogram script to use as a test-bed. Run GIMP from a terminal window and you should get 4 lines of diagnostic output when the script is run (ignore the error that follows this output - its just because it doesn't save the histogram and returns a result in an unexpected format).

On Linux Mint 22.3 Cinnamon I get the following output when selected the Documents folder

For get_url               file:///home/un/Documents
For get_path            /home/un/Documents
For get_parse_name /home/un/Documents
For get_basename    Documents


Attached Files
.7z   histogram-export2.7z (Size: 3.53 KB / Downloads: 3)
Reply
#9
Thanks, I'll take a look. But as an aside, I am now wondering if this might be a difference between the Windows and Linux platforms.
Reply
#10
Quote:On Linux Mint 22.3 Cinnamon I get the following output when selected the Documents folder

For get_url               file:///home/un/Documents
For get_path            /home/un/Documents
For get_parse_name /home/un/Documents
For get_basename    Documents


Yes, checked it all now. I get the same as you - on Linux (Ubuntu 24.04.3). I just don't on Windows 11. Suspect it may therefore be a bug.

UPDATE:

Reported it as bug #15734.
Reply


Forum Jump: