简体   繁体   中英

How do I access a variable value set by me in environment variables under system properties?

I created a new variable "sam" under system environment variables for current account and gave it a value as: "D:/My_folder/files/items". I need to fetch this path using python code. How can I do this? The code snippet below just returns my home directory:

print(os.environ['HOME'])

You can access Environment variables directly through os.environ to get complete list:

print(os.environ)

To access a specific path you need to enter a key from the path, just any word that exists in it. In your case, you can use "My_folder" for example. As follows:

print(os.environ['My_folder'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM