6

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!

5 Answers 5

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)
1
  • I get this warning: PyGIDeprecationWarning: GLib.USER_DIRECTORY_PICTURES is deprecated; use GLib.UserDirectory.DIRECTORY_PICTURES instead
    – Flimm
    Oct 28, 2023 at 13:52
6

Install the python library to access freedesktop.org standards

To get the XDG directories from Python you need to install PyXDG. You can install it using APT (depending on your python version) using one of the following packages:

For Python 2.x

sudo apt-get install python-xdg

For Python 3:

sudo apt-get install python3-xdg

You can also install it from PyPI.

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")
6
  • Thanks @Sylvain, but it appears not to work :O paste.ubuntu.com/7352802 I need $HOME/Documents, $HOME/Pictures, etc.
    – Costales
    Apr 28, 2014 at 14:38
  • 1
    I've updated my answer to parse those values Apr 28, 2014 at 16:44
  • Thanks @Sylvain Pineau! Then there isn't a solution without parse the file?
    – Costales
    Apr 28, 2014 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. Apr 28, 2014 at 17:38
  • In Python 3, ConfigParser has been renamed to configparser for PEP 8 compliance.
    – user278849
    Mar 9, 2017 at 1:35
0

I am still seeing this issue on Ubuntu 22.04 in December 2022.

What I realized is that

/usr/lib/python3.10 and ~/.local/lib/python3.10 

don't have a dist-packages or xdg. But /usr/lib/python3/dist-packages/ has xdg which has the BaseDirectory.py and others.

What ended up working for me was copying the file to my home path:

cp /usr/lib/python3/dist-packages/xdg/BaseDirectory.py ~/.local/lib/python3.10/site-packages/xdg/
0
import os
XDG_DATA_DIRS = os.environ.get("XDG_DATA_DIRS")

Repeat for your other XDG directories.

0

If you don't want to install any dependencies, you can follow the spec to write your own code, or copy the code found in BaseDirectory.py. The code is pretty straight-forward:

import os

_home = os.path.expanduser('~')

xdg_data_home = os.environ.get('XDG_DATA_HOME') or \
            os.path.join(_home, '.local', 'share')

xdg_config_home = os.environ.get('XDG_CONFIG_HOME') or \
            os.path.join(_home, '.config')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .