Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This may seem stupid to a few, but I was wondering if there was some code to find out the operating system of the present user, and then the path to his Desktop using python.

share|improve this question
    
There are a few answers in [this Stackoverflow question][1]. [1]: stackoverflow.com/questions/7403918/… –  brousch May 18 '12 at 13:17
add comment

1 Answer 1

up vote 3 down vote accepted

In Python 2.7 you can use the following commands

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import os
import platform

userhome = os.path.expanduser('~')
desktop = userhome + '/Desktop/'
useros = platform.system() # returns e.g. 'Linux' 'Windows' 
distribution = platform.linux_distribution() 'in case it's a Unix
share|improve this answer
    
This is sweet. Works like a charm on Ubuntu. But will this print the correct path to the desktop even if its a Windows system? –  Shyam K May 18 '12 at 15:27
    
Path to the windows /Desktop/-analogue will be different. Nevertheless will the above command give you the path to the current user's HOME on Windows too. Can't test this further here due to a tremendous lack of Windows - you may need some experimenting ;) –  Takkat May 18 '12 at 16:54
    
/Desktop/ is not always right when exists l18n folder. For example, for spanish is /Escritorio/. This is taken from XDG_DESKTOP_DIR in ~/.config/user-dirs.dirs –  shakaran Mar 31 '13 at 22:59
    
@shakaran: thank you for this useful note. You may try to get os.environ['XDG_DESKTOP_DIR']. Note that if it is not defined (which may likely be the case) this will throw an error on your user. –  Takkat Apr 1 '13 at 7:50
    
@Takkat I know it. Currently I am using subprocess.check_output(['xdg-user-dir', 'DESKTOP']) for detect the values. You can see a live example on my project "pygifme" in github: github.com/shakaran/pygifme/blob/master/pygifme.py#L193 I hope that helps to others –  shakaran Apr 1 '13 at 8:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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