Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 14405  / 2 Years ago, sun, february 27, 2022, 9:20:47

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.


More From » windows

 Answers
0

On a default Ubuntu running Python 2.7 you can use the following commands



#!/usr/bin/env python

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


When coding a cross platfrom application you should be aware that not all desktop environments make use of a desktop directory. This will even be true for some XDG desktops.




  • Our code should therefore not rely on the presence of a desktop directory.



In case you code for different locales you need to parse the name given for the desktop directory from the users's xdg desktop configuration (see How can I get the (XDG) Default User Directories from Python?).


[#38305] Monday, February 28, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tionpromoti

Total Points: 14
Total Questions: 112
Total Answers: 113

Location: Tonga
Member since Tue, Nov 30, 2021
2 Years ago
;