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 open it from the Start menu (Windows System → File Explorer). Or, press the keyboard shortcut Windows key + E (hold down the Windows key and press E). Click in the location bar. Type %USERPROFILE% and press Enter .
The user-profile folder is a container for applications and other system components to populate with sub-folders, and per-user data such as documents and configuration files. Windows Explorer uses the user-profile folders extensively for such items as the user's Desktop, Start menu and Documents folder.
The system special folders are folders such as Program Files, Programs, System, or Startup, which contain common information. Special folders are set by default by the system, or explicitly by the user, when installing a version of Windows. The Environment.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With