0

I am writing scripts that scrape files contained within a specific folder.

To remove the need for modifying the static path to the user's working directory, is there a way for the script to discover the user's operating system version (Windows Version or Linux), and respective Desktop file path?

4
  • Which path do you want to get? Commented Jul 14, 2017 at 19:15
  • 1
    Your question is not detailed enough, and does not show a lot of search efforts on your side. Please consider reviewing the web and then come back here with your failed code attempts. Commented Jul 14, 2017 at 19:15
  • If you just want to know the operating system, use sys.platform. And your question doesn't provide enough detail Commented Jul 14, 2017 at 19:16
  • Coldspeed - The environment is running multiple different OS. Everything from Vista to Windows 10. I was hoping to find a way to dynamically learn what the Desktop Path variable is set to. (i.e. the Windows Desktop). Since I'm already able to find the home directory in Linux, I'm also trying to figure out how to determine which OS is running and build out different configuration settings based on the environment. Commented Jul 14, 2017 at 19:22

1 Answer 1

3

You can use the 'HOMEPATH' environment variable, so the full 'Desktop' location would be

os.path.join(os.environ['HOMEPATH'], 'Desktop')
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks CoryKramer! That is EXACTLY what I was asking about.
Users or administrators can relocate the "Desktop" folder. A correct implementation uses the Windows API to query the location, either FOLDERID_Desktop or the older CSIDL_DESKTOP.
Also, the default desktop location is %USERPROFILE%\Desktop, since the value of USERPROFILE is fixed. The environment variables HOMEDRIVE and HOMEPATH can refer to an unrelated directory, as defined in the user account profile. And in some contexts (e.g. runas.exe or a scheduled task that runs with an S4U logon) their value is either wrong (e.g. points to %SystemRoot%\System32) or isn't even defined.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.