Reading in NT Variables / Login Script

Bengt Richter bokr at
Thu Jun 5 18:29:12 CEST 2003

On 5 Jun 2003 01:45:13 -0700, tony at (Tony) wrote:

>c42 <nospam at> wrote in message news:<MPG.1947a861dc883f29989680 at>...
>> What variables are you looking for?
>%USERNAME% in particualr.  But others would be useful.
os.environ acts like a case-insensitive dictionary of environment variables

 >>> import os
 >>> os.environ.get('username', '(username unknown)')
 >>> os.environ['username']
 >>> os.environ['USERNAME']
 >>> os.environ.get('USERNAME', '(username unknown)')
 >>> os.environ['USERNAME']
 >>> os.environ['PATHEXT']
 >>> os.environ['prompt']
 '[$T$H$H$H$H$H$H] $P$G'
 >>> os.environ['FOO']
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "D:\python22\lib\", line 379, in __getitem__
 KeyError: FOO

(You can see how the case-insensitivity came about)

Using .get avoids an exception if the environment variable is not defined:

 >>> os.environ.get('foo', "default doesn't have to be a string".split())
 ['default', "doesn't", 'have', 'to', 'be', 'a', 'string']


Bengt Richter

More information about the Python-list mailing list