Env Variables in Python (v3.0) on Windows

I am using Python 3.0.

How to extend the environment variable, given the syntax% var_name%?

Any help is much appreciated! Thanks!

+3
source share
2 answers

This is in a slightly unexpected place: os.path.expandvars () . Admittedly, it is pretty often used to handle paths:

>>> import os.path
>>> os.path.expandvars('%APPDATA%\\MyApp')
'C:\\Documents and Settings\\Administrator\\Application Data\\MyApp'

but it really is a shell function.

+3
source

I assume that you mean "How to get environment variables?":

import os
username = os.environ['UserName']

Alternatively you can use:

username = os.getenv('UserName')

And to add / change your own variables, you can use:

os.putenv('MyVar', 'something I want to store')
+2
source

Source: https://habr.com/ru/post/1705994/


All Articles