Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 11582  / 1 Year ago, mon, march 13, 2023, 11:45:54

By example,
In English the default user folders will be:



$HOME/Desktop
....
$HOME/Videos


In Spanish the default user folders will be:



$HOME/Escritorio
....
$HOME/Vídeos


The file ~/.config/user-dirs.dirs has those localize names for the default folders. (Always?).



I need to get those names from a python script. I'd like not to parse that file. Is there other elegant way?
I found this:



from xdg.BaseDirectory import *
print xdg_cache_home
print xdg_config_dirs
print xdg_config_home
print xdg_data_dirs
print xdg_data_home


But it doesn't return those folders.



Thanks in advance!


More From » python

 Answers
7

If you don't mind getting a dependency on GLib or if you're already using GTK as toolkit, you can use the GLib.get_user_special_dir() method.



>>> from gi.repository import GLib
>>> GLib.get_user_special_dir(GLib.USER_DIRECTORY_PICTURES)
'/home/timo/Afbeeldingen'
>>> GLib.get_user_special_dir(GLib.USER_DIRECTORY_DOCUMENTS)
'/home/timo/Documenten'
>>> GLib.get_user_special_dir(GLib.USER_DIRECTORY_DOWNLOAD)
'/home/timo/Downloads'


All available directories:



G_USER_DIRECTORY_DESKTOP         the user's Desktop directory
G_USER_DIRECTORY_DOCUMENTS the user's Documents directory
G_USER_DIRECTORY_DOWNLOAD the user's Downloads directory
G_USER_DIRECTORY_MUSIC the user's Music directory
G_USER_DIRECTORY_PICTURES the user's Pictures directory
G_USER_DIRECTORY_PUBLIC_SHARE the user's shared directory
G_USER_DIRECTORY_TEMPLATES the user's Templates directory
G_USER_DIRECTORY_VIDEOS the user's Movies directory
G_USER_N_DIRECTORIES the number of enum values


If you get this error message:




ImportError: When using gi.repository you must not import static
modules like "gobject". Please change all occurrences of "import
gobject" to "from gi.repository import GObject".




You need to use this:



import glib
return glib.get_user_special_dir(glib.USER_DIRECTORY_PICTURES)

[#25587] Tuesday, March 14, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
truwom

Total Points: 101
Total Questions: 99
Total Answers: 100

Location: Ivory Coast
Member since Tue, Sep 15, 2020
4 Years ago
;