windows 在 Python 中查找系统文件夹位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2063508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find system folder locations in Python
提问by Mr_Chimp
I am trying to find out the location of system folders with Python 3.1. For example "My Documents" = "C:\Documents and Settings\User\My Documents", "Program Files" = "C:\Program Files" etc etc.
我正在尝试使用 Python 3.1 找出系统文件夹的位置。例如“我的文档”=“C:\Documents and Settings\User\My Documents”、“Program Files”=“C:\Program Files”等。
回答by Mr_Chimp
I found a slightly different way of doing it. This way will give you the location of various system folders and uses real words instead of CLSIDs.
我发现了一种稍微不同的方法。这种方式将为您提供各种系统文件夹的位置,并使用真实的单词而不是 CLSID。
import win32com.client
objShell = win32com.client.Dispatch("WScript.Shell")
allUserDocs = objShell.SpecialFolders("AllUsersDesktop")
print allUserDocs
Other available folders: AllUsersDesktop, AllUsersStartMenu, AllUsersPrograms, AllUsersStartup, Desktop, Favorites, Fonts, MyDocuments, NetHood, PrintHood, Recent, SendTo, StartMenu, Startup & Templates
其他可用文件夹:AllUsersDesktop、AllUsersStartMenu、AllUsersPrograms、AllUsersStartup、Desktop、Favorites、Fonts、MyDocuments、NetHood、PrintHood、Recent、SendTo、StartMenu、Startup & Templates
回答by cdiggins
In Windows 7 I can use the following environment variables to access the folders I need:
在 Windows 7 中,我可以使用以下环境变量来访问我需要的文件夹:
>>> import os
>>> os.environ['USERPROFILE']
'C:\Users\digginc'
>>> os.environ['PROGRAMFILES']
'C:\Program Files'
回答by Dominic Rodger
To get the "My Documents" folder, you can use:
要获取“我的文档”文件夹,您可以使用:
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
From here.
从这里开始。
I'm not sure what the equivalent magic incantation is for "Program Files", but that should hopefully be enough to get you started.
我不确定“程序文件”的等效魔法咒语是什么,但希望这足以让您入门。
回答by Eric Smith
The Windows API call for doing this, from Vista on, is SHGetKnownFolderPath. There is an MIT-licensed wrapper (using ctypes, so no dependencies on pywin32) here.
从 Vista 开始,执行此操作的 Windows API 调用是SHGetKnownFolderPath。有一个MIT许可的包装(使用ctypes的,所以对pywin32没有依赖性)在这里。
>>> from knownpaths import *
>>> get_path(FOLDERID.ProgramFilesX86)
u'C:\Program Files (x86)'
回答by matt wilkie
Here's an alternative win32com approach because WScript.Shell
"special folders do not work in all language locales, a preferred method is to query the value from User Shell folders"(ref):
这是另一种 win32com 方法,因为WScript.Shell
“特殊文件夹不适用于所有语言区域,首选方法是从 User Shell 文件夹中查询值”( ref):
>>> 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'
The ID number comes from MSDN ShellSpecialFolderConstants Enumeration. I converted that list to csv for easy use and wrote a short python script demoing that, gist here.
ID 号来自 MSDN ShellSpecialFolderConstants Enumeration。我将该列表转换为 csv 以方便使用,并编写了一个简短的 Python 脚本进行演示,要点在这里。
Special thanks to Mr Chimp for starting this off. I relied heavily on his answer and references to get started.
特别感谢 Chimp 先生开始这件事。我非常依赖他的回答和参考资料来开始。