Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Windows' special folders for currently logged-in user?

Tags:

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.

like image 410
Primoz Avatar asked Oct 04 '10 20:10

Primoz


People also ask

How to find Users folder Windows?

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 .

What is USERPROFILE in Windows?

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.

What is system special 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.


1 Answers

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' 
like image 111
bobince Avatar answered Nov 04 '22 20:11

bobince