我试图用Python 3.1找出系统文件夹的位置。例如“我的文档”=“C:\ Documents and Settings \ User \ My Documents”,“Program Files”=“C:\ Program Files”等等。
答案 0 :(得分:8)
我找到了a slightly different way of doing it。这种方式将为您提供各种系统文件夹的位置,并使用真实的单词而不是CLSID。
import win32com.client
objShell = win32com.client.Dispatch("WScript.Shell")
allUserDocs = objShell.SpecialFolders("AllUsersDesktop")
print allUserDocs
其他可用文件夹: AllUsersDesktop,AllUsersStartMenu,AllUsersPrograms,AllUsersStartup,桌面,收藏夹,字体,MyDocuments,NetHood,PrintHood,Recent,SendTo,StartMenu,Startup&模板
答案 1 :(得分:5)
在Windows 7中,我可以使用以下环境变量来访问我需要的文件夹:
>>> import os
>>> os.environ['USERPROFILE']
'C:\\Users\\digginc'
>>> os.environ['PROGRAMFILES']
'C:\\Program Files'
答案 2 :(得分:4)
要获取“我的文档”文件夹,您可以使用:
from win32com.shell import shell
df = shell.SHGetDesktopFolder()
pidl = df.ParseDisplayName(0, None,
"::{450d8fba-ad25-11d0-98a8-0800361b1103}")[1]
mydocs = shell.SHGetPathFromIDList(pidl)
print mydocs
来自here。
我不确定“程序文件”的等效魔法是什么,但这应该足以让你开始。
答案 3 :(得分:0)
从Vista开始执行此操作的Windows API调用是SHGetKnownFolderPath。有一个MIT许可的包装器(使用ctypes,所以不依赖于pywin32)here。
>>> from knownpaths import *
>>> get_path(FOLDERID.ProgramFilesX86)
u'C:\\Program Files (x86)'
答案 4 :(得分:0)
这是另一种win32com方法,因为WScript.Shell
“特殊文件夹在所有语言环境中均不起作用,首选方法是从User Shell文件夹中查询值” ({{3} }):
>>> ID = 48
>>> shapp = win32com.client.Dispatch("Shell.Application")
>>> shapp.namespace(ID).self.path
'C:\\Users\\mattw\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Administrative Tools'
ID号来自MSDN ref。我将该列表转换为csv以便于使用,并编写了一个简短的Python脚本来演示ShellSpecialFolderConstants Enumeration。
特别感谢黑猩猩先生开始了这项工作。我非常依赖他的回答和参考来开始使用。