简体   繁体   English

如何在系统属性下的环境变量中访问由我设置的变量值?

[英]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". 我在当前帐户的系统环境变量下创建了一个新的变量“sam”,并给它一个值:“D:/ My_folder / files / items”。 I need to fetch this path using python code. 我需要使用python代码获取此路径。 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: 您可以直接通过os.environ访问环境变量以获取完整列表:

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. 在您的情况下,您可以使用“My_folder”作为示例。 As follows: 如下:

print(os.environ['My_folder'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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