5

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!

4

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)
5

Install the python library to access freedesktop.org standards

To get the XDG directories from Python you need to install (depending on your python version) one of the following packages:

For Python 2.x

sudo apt-get install python-xdg

For Python 3:

sudo apt-get install python3-xdg 

XDG Directories

  • $XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored. If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.

  • $XDG_CONFIG_HOME defines the base directory relative to which user specific configuration files should be stored. If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.

  • $XDG_DATA_DIRS defines the preference-ordered set of base directories to search for data files in addition to the $XDG_DATA_HOME base directory. The directories in $XDG_DATA_DIRS should be separated with a colon ':'.

    If $XDG_DATA_DIRS is either not set or empty, a value equal to /usr/local/share/:/usr/share/ should be used.

  • $XDG_CONFIG_DIRS defines the preference-ordered set of base directories to search for configuration files in addition to the $XDG_CONFIG_HOME base directory. The directories in $XDG_CONFIG_DIRS should be seperated with a colon ':'.

    If $XDG_CONFIG_DIRS is either not set or empty, a value equal to /etc/xdg should be used.

Source: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html

UPDATE: How to access Ubuntu specific XDG user directories:

The ~/.config/user-dirs.dirs file will always contain the localize names as the xdg-user-dirs-update tool controls this file, see the dedicated man page section:

   The XDG user dirs configuration is stored in the user-dirs.dir
   file in the location pointed to by the XDG_CONFIG_HOME
   environment variable.

The following python code will allow you to parse the ~/.config/user-dirs.dirs.

#!/usr/bin/env python

import io
import os
import re
import ConfigParser

from xdg.BaseDirectory import xdg_config_home

config = ConfigParser.RawConfigParser(allow_no_value=True)

f = open(os.path.join(xdg_config_home, "user-dirs.dirs"))
user_config = "[XDG_USER_DIR]\n" + f.read()
f.close()
user_config = re.sub('\$HOME', os.path.expanduser("~"), user_config)
user_config = re.sub('"', '', user_config)

config.readfp(io.BytesIO(user_config))
print config.get("XDG_USER_DIR", "XDG_PICTURES_DIR")
  • Thanks @Sylvain, but it appears not to work :O paste.ubuntu.com/7352802 I need $HOME/Documents, $HOME/Pictures, etc. – Costales Apr 28 '14 at 14:38
  • 1
    I've updated my answer to parse those values – Sylvain Pineau Apr 28 '14 at 16:44
  • Thanks @Sylvain Pineau! Then there isn't a solution without parse the file? – Costales Apr 28 '14 at 17:36
  • 1
    There's no dedicated python module except the one providing the original freedesktop values so I guess that parsing this file is the only solution. – Sylvain Pineau Apr 28 '14 at 17:38
  • In Python 3, ConfigParser has been renamed to configparser for PEP 8 compliance. – user278849 Mar 9 '17 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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