PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 25, 2022

[FIXED] How to know where is windows installed with Python?

 November 25, 2022     module, python, python-3.x, windows     No comments   

Issue

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.


Solution

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.)



Answered By - Roel Schroeven
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing