I am writing a program that needs to know which Drive is Windows drive . because I need to go to "Windows-Drive:\Users\publick\Desktop", I need some code or module that will give me the windows drive. Thanks for your kindness.
-
1Does this answer your question? [How can I find where Python is installed on Windows?](https://stackoverflow.com/questions/647515/how-can-i-find-where-python-is-installed-on-windows) – PacketLoss Sep 21 '22 at 13:40
-
Run py -v in cmd. – YJR Sep 21 '22 at 14:00
3 Answers
If what you really want is the drive where Windows is installed, use
import os
windows_drive = os.environ['SystemDrive']
But it looks like what you actually need is the public desktop folder. You can get that easier and more reliably like this:
import os
desktop_folder = os.path.join(os.environ['PUBLIC'], 'Desktop')
For the current user's desktop folder instead, you can use this:
import os
desktop_folder = os.path.join(os.environ['USERPROFILE'], 'Desktop')
But I'm not sure that the desktop folder is always called 'Desktop' and that it's always a subdirectory of the profile folder.
There is a more reliable way using the pywin32 package (https://github.com/mhammond/pywin32), but it obviously only works if you install that package:
import win32comext.shell.shell as shell
public_desktop_folder = shell.SHGetKnownFolderPath(shell.FOLDERID_PublicDesktop)
user_desktop_folder = shell.SHGetKnownFolderPath(shell.FOLDERID_Desktop)
(Unfortunately pywin32 is not exactly well documented. It's mostly a matter of first figuring out how to do things using Microsoft's Win32 API, then figuring out where to find the function within pywin32.)

- 1,778
- 11
- 12
-
Thanks for your kindness. Yes it is better. Also I have to say that I checked your first code (Which is written by os module) and it has worked. So yeah It was so helpfull. – Aliakbar SMMS Sep 22 '22 at 13:58
#!/usr/bin/env python3
# Python program to explain os.system() method
# importing os module
import os
# Command to execute
# Using Windows OS command
cmd = 'echo %WINDIR%'
# Using os.system() method
os.system(cmd) # check if returns the directory [var = os.system(cmd) print(var) ] in Linux it doesnt
# Using os.popen() method
return_value = os.popen(cmd).read()
can you check this approach ? I am not on Win

- 2,181
- 3
- 14
- 30
-
1
-
1@ pippo1980 %SystemDrive% is works better but they returns "C:\\" and then 0. I don't why; but it was done. But anyway I thank you and appreciate you. Thanks! – Aliakbar SMMS Sep 22 '22 at 14:13
-
https://stackoverflow.com/questions/39193953/using-systemdrive-in-python Using %systemdrive% in python – pippo1980 Sep 22 '22 at 15:54
I found my Answer!
`import os
Windows-Drive=os.path.dirname(os.path.dirname(os.path.join(os.path.join(os.environ['USERPROFILE']))))
This code is answer!

- 23
- 5