Strange result on os.environ
Eric Brunel
eric.brunel at pragmadev.com
Thu May 16 13:41:26 EDT 2002
Goncalo Rodrigues wrote:
> Hi,
>
> os.environ is supposed to be a mapping with keys the environment
> variables. Then what does it mean the following?
>
> PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on
> win32.
> Portions Copyright 1994-2001 Mark Hammond (mhammond at skippinet.com.au) -
> see 'Help/About PythonWin' for further copyright information.
>>>> import os
>>>> for key in os.environ:
> ... print key
The os.environ object behaves like a dictionary, so you should do:
for key in os.environ.keys():
to get the result you expect.
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> File "C:\Python22\lib\os.py", line 387, in __getitem__
> return self.data[key.upper()]
> AttributeError: 'int' object has no attribute 'upper'
>>>>
Interesting... Apparently, when you do a:
for key in os.environ:
it tries to do a os.environ[0], i.e. os.environ.__getitem__(0), which
executes "return self.data[key.upper()]" with key = 0. Hence the error "int
has no attribute upper"... One may say logical, but I understand it may be
confusing! Maybe the os module on Windows could be protected against this
(testing that the key is actually a string before calling "upper", for
example...).
HTH
--
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
More information about the Python-list
mailing list