[CPyUG] 如何获取当前系统的桌面的路径

1 view
Skip to first unread message

弓虽!

unread,
Aug 16, 2011, 8:06:28 AM8/16/11
to python-cn
如何获取当前系统的桌面的路径

--
来自: python-cn`CPyUG`华蟒用户组(中文Python技术邮件列表)
发言: pyth...@googlegroups.com
退订: python-cn+...@googlegroups.com (向此发空信即退!)
详情: http://code.google.com/p/cpyug/wiki/PythonCn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
强烈: 建议使用技巧: 如何有效地报告Bug
http://www.chiark.greenend.org.uk/%7Esgtatham/bugs-cn.html

Jimmy Kuu

unread,
Aug 16, 2011, 8:47:20 AM8/16/11
to pyth...@googlegroups.com
看你前面问得问题应该是在Windows环境下
Google一下: python+桌面路径

from win32com.shell import shell, shellcon

def GetDesktopPath():
    ilist = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
    dtpath = shell.SHGetPathFromIDList(ilist)
    return dtpath

弓虽!

unread,
Aug 16, 2011, 9:22:13 AM8/16/11
to pyth...@googlegroups.com
哦,那看来只有这个了,我以为有更好的方法。

弓虽!

unread,
Aug 16, 2011, 9:24:07 AM8/16/11
to pyth...@googlegroups.com
我干脆这样算了
os.path.expanduser("~") + u"\\桌面\\"
反正我的用户都是中文windows系统,呵呵
os.path.expanduser("~") 这个方法,标准吗?

依云

unread,
Aug 17, 2011, 2:01:24 AM8/17/11
to pyth...@googlegroups.com
On Tue, Aug 16, 2011 at 09:24:07PM +0800, 弓虽! wrote:
> 我干脆这样算了
> os.path.expanduser("~") + u"\\桌面\\"
> 反正我的用户都是中文windows系统,呵呵
> os.path.expanduser("~") 这个方法,标准吗?

文档里有解释的:

On Windows, HOME and USERPROFILE will be used if set, otherwise
a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user
is handled by stripping the last directory component from the created
user path derived above.

--
Best regards,
lilydjwg

Linux Vim Python 我的博客
http://lilydjwg.is-programmer.com/

leo

unread,
Aug 17, 2011, 2:16:36 AM8/17/11
to pyth...@googlegroups.com
你遇到英文系统怎么办?
这种方法不好.

--
chlin

依云

unread,
Aug 17, 2011, 2:26:40 AM8/17/11
to pyth...@googlegroups.com
On Wed, Aug 17, 2011 at 02:16:36PM +0800, leo wrote:
> 你遇到英文系统怎么办?
> 这种方法不好.
>
> On 2011-8-16 21:24, 弓虽! wrote:
> > 我干脆这样算了
> > os.path.expanduser("~") + u"\\桌面\\"
> > 反正我的用户都是中文windows系统,呵呵
> > os.path.expanduser("~") 这个方法,标准吗?

不光英文的会出问题。就算是中文的,“桌面”文件夹也可以设置为其它目录的(比
如放到 D 盘对习惯把桌面当临时文件夹的人来说是个非常好的主意)。

--
Best regards,
lilydjwg

Linux Vim Python 我的博客
http://lilydjwg.is-programmer.com/

弓虽!

unread,
Aug 17, 2011, 8:35:45 AM8/17/11
to pyth...@googlegroups.com
额~
这英文什么意思

弓虽!

unread,
Aug 17, 2011, 8:37:37 AM8/17/11
to pyth...@googlegroups.com
那只能用jimmy kuu说的那种方法啦?

弓虽!

unread,
Aug 17, 2011, 9:05:15 AM8/17/11
to pyth...@googlegroups.com
找到另一种方法
import win32api,win32con
def get_desktop():
key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,\
r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders',\
0,win32con.KEY_READ)
return win32api.RegQueryValueEx(key,'Desktop')[0]

Wang Xuerui

unread,
Aug 17, 2011, 9:49:36 AM8/17/11
to pyth...@googlegroups.com
在 2011年8月17日 下午9:05,弓虽! <thi...@gmail.com> 写道:
> 找到另一种方法
> import win32api,win32con
> def get_desktop():
> key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,\
> r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
> Folders',\
> 0,win32con.KEY_READ)
> return win32api.RegQueryValueEx(key,'Desktop')[0]

窃以为光是读注册表的话,用_winreg模块就可以了,没必要引入win32api依赖关系的。。。

当然如果你其他地方依赖win32api这样写就可以接受- -

弓虽!

unread,
Aug 17, 2011, 11:08:41 AM8/17/11
to pyth...@googlegroups.com
哇 不知道有这个模块,搜索python+注册表只看到win32pai的,还是出自《征服python》的节选,就用了win32api

Kabie

unread,
Aug 18, 2011, 6:26:09 AM8/18/11
to pyth...@googlegroups.com
import os
os.path.join(os.environ['HOME'], 'Desktop')

当然有的会本地化Desktop……得再判断一下……

在 2011年8月16日 下午8:06,弓虽! <thi...@gmail.com>写道:

greatghoul

unread,
Aug 18, 2011, 6:44:44 AM8/18/11
to pyth...@googlegroups.com
http://zaichunchen.appspot.com/get_user_desktop_path_from_python.html

from win32com.shell import shell

def GetDesktoppath():
    ilist = shell.SHGetSpecialFolderLocation(0,shellcon.CSIDL_DESKTOP)
    dtpath = shell.SHGetPathFromIDList(ilist)
    return dtpath
2011/8/18 Kabie <kabi...@gmail.com>



--
Ghoul To World! -  http://www.g2w.me
ScriptFan - http://scriptfan.com/

Jiahua Huang

unread,
Aug 18, 2011, 8:56:10 AM8/18/11
to python-cn`CPyUG`华蟒用户组(中文Python技术邮件列表)
中文 Win7 也会有问题吧

yang guang

unread,
Aug 18, 2011, 9:03:46 AM8/18/11
to pyth...@googlegroups.com
在 2011年8月18日 下午6:26,Kabie <kabi...@gmail.com> 写道:
> import os
> os.path.join(os.environ['HOME'], 'Desktop')
> 当然有的会本地化Desktop……得再判断一下……
>

Win7没有本地化问题,中英文操作系统都是Desktop。

Reply all
Reply to author
Forward
0 new messages