How can I get Windows special folders like My Documents
, Desktop
, etc. from my Python script? Do I need win32 extensions?
It must work on Windows 2000 to Windows 7.
How can I get Windows special folders like My Documents
, Desktop
, etc. from my Python script? Do I need win32 extensions?
It must work on Windows 2000 to Windows 7.
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)
Check shellcon.CSIDL_xxx
for other possible folders.
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).
win32com.shell
module contains functions from shell32.dll. –
Saltire SHGFP_TYPE_CURRENT
and SHGFP_TYPE_DEFAULT
for the last parameter of SHGetFolderPath()
, but these aren't defined in shellcon
for some reason... I wonder why not. –
Diophantus df = shell.SHGetDesktopFolder() pidl = df.ParseDisplayName(0, None, "::{450d8fba-ad25-11d0-98a8-0800361b1103}")[1] mydocs = shell.SHGetPathFromIDList(pidl)
–
Dwan ctypes
isn't easy to get right. Luckily someone provided a correct (I guess) answer, but manually writing calls to lots of C functions and getting the data right can be non-trivial. No huge difference otherwise and actually ctypes usage will remove one required dependency. –
Saltire Should you wish to do it without the win32 extensions, you can use ctypes
to call SHGetFolderPath:
>>> import ctypes.wintypes
>>> CSIDL_PERSONAL= 5 # My Documents
>>> SHGFP_TYPE_CURRENT= 0 # Want current, not default value
>>> buf= ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
>>> ctypes.windll.shell32.SHGetFolderPathW(0, CSIDL_PERSONAL, 0, SHGFP_TYPE_CURRENT, buf)
0
>>> buf.value
u'C:\\Documents and Settings\\User\\My Documents'
ctypes
instead of the win32
extensions, namely the fact that the former doesn't supply any of the often needed Window's constants like those defined in win32com.shellcon
-- so one has to look-up their values and add each of them manually. –
Acetabulum deprecated
. I'm not sure what are the implications... –
Keiko ctypes.windll.shell32.SHGetFolderPathW(0, CSIDL_PERSONAL, 0, SHGFP_TYPE_CURRENT, buf)
twice. The first time I got error code -2147024890
. –
Petiolule deprecated
: The SHGetKnownFolderPath is much more cumbersome to use, so I would use win32com in that case. Using the answer here for now, to keep down my dependencies :). –
Yellows SHGetFolderPathW()
is already a wrapper to SHGetKnownFolderPath()
since Windows Vista, this will possibly be available in the foreseeable future, deprecated or not... –
Joleen 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)
Check shellcon.CSIDL_xxx
for other possible folders.
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).
win32com.shell
module contains functions from shell32.dll. –
Saltire SHGFP_TYPE_CURRENT
and SHGFP_TYPE_DEFAULT
for the last parameter of SHGetFolderPath()
, but these aren't defined in shellcon
for some reason... I wonder why not. –
Diophantus df = shell.SHGetDesktopFolder() pidl = df.ParseDisplayName(0, None, "::{450d8fba-ad25-11d0-98a8-0800361b1103}")[1] mydocs = shell.SHGetPathFromIDList(pidl)
–
Dwan ctypes
isn't easy to get right. Luckily someone provided a correct (I guess) answer, but manually writing calls to lots of C functions and getting the data right can be non-trivial. No huge difference otherwise and actually ctypes usage will remove one required dependency. –
Saltire import win32com.client
oShell = win32com.client.Dispatch("Wscript.Shell")
print oShell.SpecialFolders("Desktop")
Try winshell (made exactly for this purpose):
import winshell
print 'Desktop =>', winshell.desktop ()
print 'Common Desktop =>', winshell.desktop (1)
print 'Application Data =>', winshell.application_data ()
print 'Common Application Data =>', winshell.application_data (1)
print 'Bookmarks =>', winshell.bookmarks ()
print 'Common Bookmarks =>', winshell.bookmarks (1)
print 'Start Menu =>', winshell.start_menu ()
print 'Common Start Menu =>', winshell.start_menu (1)
print 'Programs =>', winshell.programs ()
print 'Common Programs =>', winshell.programs (1)
print 'Startup =>', winshell.startup ()
print 'Common Startup =>', winshell.startup (1)
print 'My Documents =>', winshell.my_documents ()
print 'Recent =>', winshell.recent ()
print 'SendTo =>', winshell.sendto ()
A little bit hacky, but without the need of a special import
import os
os.popen('echo %appdata%').read().strip()
os.environ
? –
Colobus https://pypi.org/project/userpaths is an alternatve for that.
# Get the user's My Documents folder
import userpaths
my_docs = userpaths.get_my_documents()
You can directly use like that.
© 2022 - 2024 — McMap. All rights reserved.