如何获得独立于语言安装(python)的python用户桌面路径 [英] How to get users desktop path in python independent of language install (linux)

查看:300
本文介绍了如何获得独立于语言安装(python)的python用户桌面路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了我如何找到用户桌面的路径"的几个问题和答案,但是在我看来,它们都是不存在的(至少我发现了).原因是,如果用户安装了非英语的Linux安装,则他或她很可能在~/Desktop以外的其他位置安装了台式机.例如.对于瑞典语,我相信它在~/Skrivbord中.谁知道用户决定将桌面放置在哪里?

I've found several questions and answers to "How do I find the path to the users desktop", but in my mind they are all defunct (those I found at least). The reason is that if the user has a installation of Linux that is not English, he or she may well have the desktop somewhere other than in ~/Desktop. E.g. for Swedish, I believe it is in ~/Skrivbord. And who knows where the user decided to place his or her desktop?

因此,以下内容将不会(或它的任何变体):

For that reason the following won't do (or any variant of it):

os.sep.join((os.path.expanduser("~"), "Desktop"))

所以问题是:

在linux上,如何在不以英语默认目录结构为前提的情况下,在python中获取用户桌面的真实路径?

On linux, how do I, in python, get the true path to the user's desktop without presupposing English default directory structure?

推荐答案

您可以直接调用xdg-user-dir实用程序,而不是直接从~/.config/user-dirs.dirs文件中进行读取:

Instead of reading directly from the ~/.config/user-dirs.dirs file you can call the xdg-user-dir utility:

import subprocess
subprocess.check_output(['xdg-user-dir', 'DESKTOP'])

这将与具有遵循freedesktop规范的DE的任何Linux发行版一起使用,这意味着:KDE,Gnome,XFCE以及更多(与OP已经发布的解决方案完全一样).

This will work with any linux distribution that has a DE which follow the freedesktop specification, which means: KDE, Gnome, XFCE and many more(exactly like the solution already posted by the OP).

更新了subprocess.check_output,因为参数需要在列表中. 还值得指出的是,您可以将DESKTOP更改为例如获取该文件夹的文档.

Updated the subprocess.check_output since arguments need to be in a list. Also worth pointing out that you can change DESKTOP to e.g. DOCUMENTS to get that folder.

这篇关于如何获得独立于语言安装(python)的python用户桌面路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆