Python,获取当前登录用户的windows特殊文件夹 [英] Python, get windows special folders for currently logged-in user

查看:67
本文介绍了Python,获取当前登录用户的windows特殊文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Python 脚本中获取 Windows 特殊文件夹,例如我的文档"、桌面"等?我需要 win32 扩展吗?

How can I get Windows special folders like My Documents, Desktop, etc. from my Python script? Do I need win32 extensions?

它必须适用于 Windows 2000 到 Windows 7.

It must work on Windows 2000 to Windows 7.

推荐答案

您可以使用 pywin32 扩展:

You can do it with the pywin32 extensions:

from win32com.shell import shell, shellcon
print shell.SHGetFolderPath(0, shellcon.CSIDL_MYPICTURES, None, 0)
# prints something like C:\Documents and Settings\Username\My Documents\My Pictures
# (Unicode object)

检查 shellcon.CSIDL_xxx 是否有其他可能的文件夹.

Check shellcon.CSIDL_xxx for other possible folders.

我认为使用 pywin32 是最好的方法.否则,您必须使用 ctypes 以某种方式访问​​ SHGetFolderPath 函数(其他解决方案也是可能的,但这些是我所知道的).

I think using pywin32 is the best way. Else you'd have to use ctypes to access the SHGetFolderPath function somehow (other solutions might be possible but these are the ones I know).

这篇关于Python,获取当前登录用户的windows特殊文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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